<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Cliff Green's Blog : CAS</title><link>http://blogs.msdn.com/cliffgreen/archive/tags/CAS/default.aspx</link><description>Tags: CAS</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Discover SharePoint Context within an Integrated SSRS report</title><link>http://blogs.msdn.com/cliffgreen/archive/2008/12/12/discover-sharepoint-context-within-an-integrated-ssrs-report.aspx</link><pubDate>Fri, 12 Dec 2008 20:09:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9202784</guid><dc:creator>green.cliff</dc:creator><slash:comments>16</slash:comments><comments>http://blogs.msdn.com/cliffgreen/comments/9202784.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cliffgreen/commentrss.aspx?PostID=9202784</wfw:commentRss><description>&lt;p&gt;With SQL Reporting Services integration with SharePoint there may be a need to be contextually aware of the site or list where your report resides.&amp;nbsp; With the ability to add code or reference custom assemblies in our report we have a starting point to gather this information and leverage information that may be available to us in a site.&amp;nbsp; The first thing I thought of when thinking through this issue was that I could leverage the &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.aspx" target="_blank"&gt;SPContext&lt;/a&gt; object.&amp;nbsp; This would provide the ability to get the current site Url and reference information that way.&amp;nbsp; Unfortunately, the reference to SPContext within the report is null because it is being rendered in an IFRAME.&amp;nbsp; So we can't use that mechanism to do that.&amp;nbsp; So the next idea was to get a reference to HttpContext and leverage that in order to find out where we are in SharePoint.&amp;nbsp; When a report runs from a SharePoint document library it runs within the RSViewerPage.aspx page.&amp;nbsp; The actual Url request in my environment is, &lt;a title="http://cliffgre-mossvm/SiteDirectory/reports/_layouts/ReportServer/RSViewerPage.aspx?RelativeReportUrl=/SiteDirectory/reports/RDL/TestReport.rdl&amp;amp;Source=http%3A%2F%2Fcliffgre%2Dmossvm%2FSiteDirectory%2Freports%2FRDL%2FForms%2FAllItems%2Easpx&amp;amp;DefaultItemOpen=0" href="http://cliffgre-mossvm/SiteDirectory/reports/_layouts/ReportServer/RSViewerPage.aspx?RelativeReportUrl=/SiteDirectory/reports/RDL/TestReport.rdl&amp;amp;Source=http%3A%2F%2Fcliffgre%2Dmossvm%2FSiteDirectory%2Freports%2FRDL%2FForms%2FAllItems%2Easpx&amp;amp;DefaultItemOpen=0"&gt;http://cliffgre-mossvm/SiteDirectory/reports/_layouts/ReportServer/RSViewerPage.aspx?RelativeReportUrl=/SiteDirectory/reports/RDL/TestReport.rdl&amp;amp;Source=http%3A%2F%2Fcliffgre%2Dmossvm%2FSiteDirectory%2Freports%2FRDL%2FForms%2FAllItems%2Easpx&amp;amp;DefaultItemOpen=0&lt;/a&gt;.&amp;nbsp; The report, however, is run using a pointer to the Report Server.&amp;nbsp; In my environment the Web Application lives in &lt;a href="http://cliffgre-mossvm"&gt;http://cliffgre-mossvm&lt;/a&gt;.&amp;nbsp; When I request a report it runs in a reference to &lt;a href="http://cliffgre-mossvm:8000/ReportServer"&gt;&lt;font color="#000000"&gt;http://cliffgre-mossvm:8000/ReportServer&lt;/font&gt;&lt;/a&gt;, so it runs completely out of the SharePoint environment.&amp;nbsp; The Url request in the report looks something like this:&lt;/p&gt; &lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;http:&lt;span style="color: #008000"&gt;//cliffgre-mossvm:8000/ReportServer/Reserved.ReportServer?http://cliffgre-mossvm/SiteDirectory/reports/RDL/TestReport.rdl&lt;/span&gt;
&amp;amp;rs:SessionID=l1ahe1yas2icsrjuiar0na55&amp;amp;rs:command=Render&amp;amp;rs:Format=HTML4.0&amp;amp;rc:HTMLFragment=&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;&amp;amp;rc:Section=1
&amp;amp;rc:StreamRoot=/SiteDirectory/reports/Reserved.ReportViewerWebPart.axd?ReportSession=l1ahe1yas2icsrjuiar0na55&amp;amp;
ControlID=05e0aa50c74646f3858bf6847b08f5f2&amp;amp;Culture=1033&amp;amp;UICulture=1033&amp;amp;ReportStack=1&amp;amp;OpType=ReportImage&amp;amp;StreamID=
&amp;amp;rc:ResourceStreamRoot=/SiteDirectory/reports/Reserved.ReportViewerWebPart.axd?ReportSession=l1ahe1yas2icsrjuiar0na55
&amp;amp;ControlID=05e0aa50c74646f3858bf6847b08f5f2&amp;amp;Culture=1033&amp;amp;UICulture=1033&amp;amp;ReportStack=1&amp;amp;OpType=ReportImage&amp;amp;ResourceStreamID=
&amp;amp;rc:ActionScript=ClientReport05e0aa50c74646f3858bf6847b08f5f2.ActionHandler&amp;amp;rc:StyleStream=&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;&amp;amp;rc:LinkTarget=_top
&amp;amp;rc:UserAgent=Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.2;+.NET+CLR+1.1.4322;+.NET+CLR+2.0.50727;+.NET+CLR+3.0.04506.30;+InfoPath.2;+.NET+CLR+3.0.04506.648;+.NET+CLR+3.5.21022)
&amp;amp;rc:Toolbar=&lt;span style="color: #0000ff"&gt;false&lt;/span&gt;&amp;amp;rs:ErrorResponseAsXml=&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;&amp;amp;rs:AllowNewSessions=false&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;One of the things that gets passed as part of the query string is the path to the report.&amp;nbsp; Notice the first line above after the question mark (?).&amp;nbsp; We can leverage that information and parse the Url in order to find out the site and list where the report lives.&amp;nbsp; So now we have the Url to the report and we can leverage the SharePoint object model to get a reference to SPSite, SPWeb and down to SPList.&amp;nbsp; Because we are running a custom assembly in reporting services it will have to be marked to AllowPartiallyTrustedCallers using the [assembly: AllowPartiallyTrustedCallers] attribute.&lt;/p&gt;
&lt;p&gt;If only things were that simple.&amp;nbsp; Once we add the code to our custom assembly and install it in the GAC we will get the error message below.&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;SecurityException: Request &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; the permission of type 
&lt;span style="color: #006080"&gt;'Microsoft.SharePoint.Security.SharePointPermission, 
Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, 
PublicKeyToken=71e9bce111e9429c'&lt;/span&gt; failed.
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In order to get around this problem we have to assert the required SharePoint permissions in order to have access to the SharePoint object model.&amp;nbsp; By surrounding our code with an Assert() for SharePointPermission our code will run and allow us to navigate the SharePoint object model as we see fit.&amp;nbsp; The full coding example is shown below.&amp;nbsp; Although it only simply returns the .PortalName and .Url properties, it can be extended to meet your needs.&lt;/p&gt;
&lt;div&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; GetInfo() {&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; siteInfo = &lt;span style="color: #006080"&gt;""&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;try&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; siteCollectionUrl = &lt;span style="color: #006080"&gt;""&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; queryString = HttpContext.Current.Request.Url.ToString();&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; indexStart = (queryString.IndexOf(&lt;span style="color: #006080"&gt;"?"&lt;/span&gt;)+1);&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; indexEnd = queryString.IndexOf( &lt;span style="color: #006080"&gt;"&amp;amp;"&lt;/span&gt; );&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; reportUrl = queryString.Substring( indexStart, (indexEnd-indexStart) );&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt;         siteCollectionUrl = reportUrl.Substring( 0, reportUrl.LastIndexOf(&lt;span style="color: #006080"&gt;"/"&lt;/span&gt;) );&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  11:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  12:&lt;/span&gt;         SharePointPermission sharepointPerm = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SharePointPermission( PermissionState.Unrestricted );&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  13:&lt;/span&gt;         sharepointPerm.Assert();&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  14:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  15:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;using&lt;/span&gt;( SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite( siteCollectionUrl ) ) {&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  16:&lt;/span&gt;             siteInfo = siteCollection.PortalName + &lt;span style="color: #006080"&gt;": "&lt;/span&gt;+ siteCollection.Url;&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  17:&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  18:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  19:&lt;/span&gt;         sharepointPerm.Deny();&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  20:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  21:&lt;/span&gt;     } &lt;span style="color: #0000ff"&gt;catch&lt;/span&gt;( Exception ex ) {&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  22:&lt;/span&gt;         siteInfo = ex.Message + ex.StackTrace;&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  23:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  24:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; siteInfo;&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  25:&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now that we have done this we have a report that can leverage the SharePoint object model as needed.&amp;nbsp; Of course this won't work within Visual Studio so the report has to be deployed to SharePoint in order for the code to run properly.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9202784" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cliffgreen/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://blogs.msdn.com/cliffgreen/archive/tags/Reporting+Services/default.aspx">Reporting Services</category><category domain="http://blogs.msdn.com/cliffgreen/archive/tags/SSRS+Code+Examples/default.aspx">SSRS Code Examples</category><category domain="http://blogs.msdn.com/cliffgreen/archive/tags/CAS/default.aspx">CAS</category></item><item><title>SharePoint Web Part Embedded Resource Permissions</title><link>http://blogs.msdn.com/cliffgreen/archive/2008/10/27/sharepoint-web-part-embedded-resource-permissions.aspx</link><pubDate>Mon, 27 Oct 2008 17:45:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9018314</guid><dc:creator>green.cliff</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/cliffgreen/comments/9018314.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cliffgreen/commentrss.aspx?PostID=9018314</wfw:commentRss><description>&lt;p&gt;It is a normal expectation for companies to embed resources like javascript, CSS and other resources in an assembly.&amp;nbsp; Web Parts are no exceptions.&amp;nbsp; For those of you that deploy everything to the GAC, you can disregard the remainder of this post.&amp;nbsp; The rest of you, bear with me.&amp;nbsp; If we are not deploying to the GAC we are typically deploying to the bin.&amp;nbsp; The bin directory in SharePoint has the following permissions by default; full control for SYSTEM, Administrators and WSS_ADMIN_WPG groups, and read-only access for WSS_WPG group.&amp;nbsp; This is not sufficient for embedded resources to be loaded by accounts that don't exists in one of these groups.&amp;nbsp; If you run an assembly with embedded resources with anything other than an account that is in one of these groups you will get the following error:&lt;/p&gt; &lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;Exception raised when trying to access embedded resources:
System.UnauthorizedAccessException: Access to the path &lt;span style="color: #006080"&gt;'C:\inetpub\wwwroot\wss\VirtualDirectories\80\bin\SampleWP.DLL'&lt;/span&gt; &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; denied. 
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
at System.IO.File.GetLastWriteTimeUtc(String path) at System.IO.File.GetLastWriteTime(String path) 
at System.Web.Handlers.AssemblyResourceLoader.GetAssemblyInfoWithAssertInternal(Assembly assembly) 
at System.Web.Handlers.AssemblyResourceLoader.GetAssemblyInfo(Assembly assembly) 
at System.Web.Handlers.AssemblyResourceLoader.GetWebResourceUrlInternal(Assembly assembly, String resourceName, Boolean htmlEncoded) 
at System.Web.Handlers.AssemblyResourceLoader.GetWebResourceUrl(Type type, String resourceName, Boolean htmlEncoded) 
at System.Web.UI.ClientScriptManager.GetWebResourceUrl(Page owner, Type type, String resourceName, Boolean htmlEncoded) 
at System.Web.UI.ClientScriptManager.RegisterClientScriptResource(Type type, String resourceName) 
at TestWebPart.ResourcePermissionTest.OnPreRender(EventArgs e)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The assembly doesn't have permission to access the bin directory.&amp;nbsp; If you don't get this error do an IIS reset and then access the web part as a user that isn't in one of these groups.&amp;nbsp; The difficult part of figuring this out was that if a user with permissions to write to the directory first accesses the web part it will render properly, not only for that user, but any user thereafter.&amp;nbsp; This will happen until the cache is exhausted.&amp;nbsp; So even during the debugging process I was wondering what the heck I just saw a couple of times.&lt;/p&gt;
&lt;p&gt;On the surface the easy solution is to change the permissions and set 'everyone' to have read / write access and move on.&amp;nbsp; What you will notice is that SharePoint will reset these permissions back to the standard after certain events.&amp;nbsp; One of those events is deploying a solution.&amp;nbsp; If you watch the directory you will notice that the bin permissions are recreated when this happens.&amp;nbsp; The bin directory is actually deleted and recreated.&amp;nbsp; So while the 'everyone' account will work temporarily, it isn't a long term solution.&lt;/p&gt;
&lt;p&gt;What about CAS?&amp;nbsp; Doesn't work.&amp;nbsp; Contrary to some of the other posts out there about this this isn't a CAS issue.&amp;nbsp; Granting the assembly 'Full' trust doesn't change the fact that an account that doesn't have access to the directory needs to write to it.&amp;nbsp; While you can set the web.config and disable impersonation or create a CAS policy that doesn't impersonate, this isn't the best solution in cases where you need to impersonate the user.&amp;nbsp; Windows Auth. comes to mind.&amp;nbsp; It would allow you to take the non-impersonated account and add them to one of the built-in groups so the web part would function properly.&amp;nbsp; This probably isn't an acceptable solution if you are an ISV or third party looking to distribute your web part to other parties.&amp;nbsp; They may not be open to adding least privilege accounts to groups on their servers &lt;/p&gt;
&lt;p&gt;One solution that does work is creating a folder in the web application directory and changing the web.config to add a &lt;a href="http://msdn.microsoft.com/en-us/library/823z9h8w.aspx" target="_blank"&gt;probingPath&lt;/a&gt; in order to help the .NET runtime locate your assembly.&amp;nbsp; By creating your own directory you control the permissions and they aren't reset by SharePoint.&amp;nbsp; Your installation and setup are a little more trouble, but you have a solution that should work long term without sacrifices to the functionality of SharePoint or, most importantly, your web part.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9018314" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cliffgreen/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://blogs.msdn.com/cliffgreen/archive/tags/Web+Parts/default.aspx">Web Parts</category><category domain="http://blogs.msdn.com/cliffgreen/archive/tags/Embedded+Resources/default.aspx">Embedded Resources</category><category domain="http://blogs.msdn.com/cliffgreen/archive/tags/CAS/default.aspx">CAS</category></item></channel></rss>