Code Access Security (CAS) is an important technology for both SharePoint administrators and developers.

But everyone that have the need to create one of this CAS policies would agree that isn't very easy to create them.

Today i've discovered that we can create this complex configuration xml using WSP Builder, and the best part is that it's easy :).

So you just need to add the Microsoft.SharePoint.Security.dll to your project, add the namespace Microsoft.SharePoint.Security and the necessary permissions to your class file.

NOTE: This will only work if you add the assembly to the bin folder of your site using WSP Builder

Here is an example:

Add the following lines to your class 

[SharePointPermissionAttribute(System.Security.Permissions.SecurityAction.Demand, ObjectModel = true)]
[SharePointPermissionAttribute(System.Security.Permissions.SecurityAction.Demand, Impersonate = true)] 
public class MyClass{….}

And the WSP Builder will create this in the solution manifest file:

<PolicyItem>
<PermissionSet class="NamedPermissionSet" version="1" Description="Permission set for MySolution">
<IPermission class="Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, version=12.0.0.0, Culture=neutral PublicKeyToken=71e9bce111e9429c" version="1" ObjectModel="True" Impersonate="True" />
… 
</PolicyItem>

LEAVE A REPLY

Please enter your comment!
Please enter your name here