Ninject with Sharepoint 2

In first acticle we talk about how we configure our Ninject with Sharepoint .Now there are two issue one of them is our ninject dlls permissions and the your assemblies that using ninject should allow partially trust callers.

When you configure your ninject dll to sharepoint you probably encounter with permission denied errors . That is normal beacuse our custom Ninject Library is using Reflection and Sharepoint libraries and you have to define CAS for this libraries .

Setup your CAS

I’m using for deployment  WSPBuilder tool and i use it with  a customCAS file.There are two CAS permission as important

  • Microsoft.SharePoint.Security.SharePointPermission
  • System.Security.Permissions.ReflectionPermission

For deployment batch file contains that:

WSPBuilder.exe -WSPName Ninject.Deploy.wsp -CustomCAS CustomCAS.txt -SolutionID [Your Solution ID]

And you should copy CustomCAS.txt to same folder of WspBuilder.exe that contains

<IPermission class=”Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c” version=”1″ ObjectModel=”True” UnsafeSaveOnGet=”True” Unrestricted=”True”  /  >

<IPermission class=”System.Security.Permissions.ReflectionPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089″ version=”1″ Flags=”3″ Unrestricted=”True” /  >

For more information WSPBuilder with custom CAS there is a simple post here:
For more info Microsoft Windows SharePoint Services and Code Access Security is Msdn

Allow Partially Trusted Callers

There is another job about your  class or page assemblies that using ninject should be contains 
[assembly: AllowPartiallyTrustedCallers()]
attribute in your AssemblyInfo.cs file . (You should add refrence of “using System.Security;” namespace)

After build you can craete a setup with Sharepoint Solution Installer using your wsp file deploy without any further error.

Happy codding…


About bpostaci
Escalation Engineer in Microsoft.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: