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

 </NamedPermissionSets>

<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]">

</IMembershipCondition>

</CodeGroup>

<!-- 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

 0024000004800000940000000602000000240000525341310004000001000100f9edc

57832372d0722d0ac9c068a482b47070d01b98ef1d7edd12eeec895e2a3d361220aef6

81d18c0cff1b2a9e606a18d968f6d4cceb148077c961d3e014a22b4049fb9c49c424f39e

6f192b27d961978ea766734432ce913b003b4085d7be21be2c570aa7c5a17506a25dac

84c2dd1ff037c22e147ab7ac563498b8602f8cf

 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.

<securityPolicy>

<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