October 28, 2009

Apply Custom Policy file to SharePoint Web Part

At the time of deployment of webpart, it is not possible for you to deploy DLL in GAC or have FULL trust level in the selected website.

In such case you can use custom policy file to come out from the problem.
Here are the steps to apply custom policy file to for your webpart(s).
·        Navigate to 12 hive\config folder.
·        Take a copy of WSS_Minimal.config file
·        Rename to [CompanyName]_minimal.config.
Open the new config file and add below code lines into the file as per given location. Add below code


<CodeGroup class="FirstMatchCodeGroup" version="1" PermissionSetName="Nothing">

<IMembershipCondition class="AllMembershipCondition" version="1" />

<!-- New Code Lines added -->

<CodeGroup class="UnionCodeGroup" version="1" PermissionSetName="FullTrust">

<IMembershipCondition class="StrongNameMembershipCondition" version="1" PublicKeyBlob="[Paste your Public Key BLOB]">



<!-- New Code Lines completed -->

Save the file. Don’t close it.

You need to add Public Key’s Blob file in addition to the above lines. To create the Blob you can follow the step below.

Open Start > All Programs > Microsoft Visual Studio 2005 > Visual Studio Tools > Visual Studio Command Prompt.

Type the following command
sn -p YourStongNameFile.snk PublicKeyOnly.snk

Where YourStongNameFile.snk is Your Projects SNK (Strong Name Key) file and PublicKeyOnly.snk is New creted PublicKey 

After you run the above command run the beloow command to create Blob:

 sn -tp PublicKeyOnly.snk

You will get your PublicKey's BLOB like below sample






 Copy this new blob and paste in config file in place the text “[Paste your Public Key BLOB]
that you kept open.

Save and close the config file.

  • Put your config file in 12 hive\Config folder.
  • Open you existing config file of your site and add one tag in config file under WSS_Medium,WSS_Minimal trust levels tags.


<trustLevel name="WSS_Medium"  policyFile="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\config\wss_mediumtrust.config" />

<trustLevel name="WSS_Minimal" policyFile="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\config\wss_minimaltrust.config" />

<!—Newly added code lines à

<trustLevel name="CompanyName_Minimal"  policyFile="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions \12\config\CompanyName_Minimal.config" />

<!—Newly added code lines end à

Now you have reached to the last step of the configuration,  change Trust Level WSS_Minimal to [CompanyName]_Minimal.

<trust level="CompanyName_Minimal" originUrl="" />

Save and close the config file.

Thats it J

If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment