Tuesday, May 6, 2008

How to grant permissions to a custom assembly that is referenced in a report in Reporting Services

It seems straight forward. Isn't it? but it may not be as simple and straight forward for the newbies as it seems at first. A colleague and a  friend of mine recently was struggling with the same issue and he learnt the lesson after spending considerable amount of time experimenting with different solutions. You get a security exception when you write your own component to be used in a report in reporting services. My friend is working on a SharePoint project that involves reporting services. He created a report that used a custom built component. The code used SharePoint object model. He tried to use Elevated privileges but even that didn't work because the error is thrown on the line that has the elevated priviliges code. He even tried the code access security, that didn't work. The following KB article has information about this problem:
 http://support.microsoft.com/kb/842419/ (How to grant permissions to a custom assembly that is referenced in a report in Reporting Services)
Use following code to get rid of the security exception:
SharePointPermission perm = new SharePointPermission(PermissionState.Unrestricted);
perm.Assert();
//Code using SharePoint object model here!!
perm.Deny();
Include following DLL in the references before you use the above code:
Microsoft.SharePoint.Security.DLL
This DLL will be found in the 12 hive. Exact location is as following:
System Drive:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI
Add following namespaces at the top:
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
That's it. Your component will now work fine.

No comments:

Post a Comment