Friday, May 17, 2013

Deal easily with Safe Controls problems in SharePoint with Visual Studio 2010

You get the following error when you try to add a custom WebPart in to your site:

"Type is not registered as safe"

Error Message

This means that you:

  1. Don't have an web.config entry for your dll
  2. have a wrong web.config entry for your dll

The 2nd reason is more often, especially when you develop a WebPart in Visual Studio and change the default Class or NameSpace names.

Steps to solve the problem

1. Check your web.config

This step is only to check if there is a web.config entry when you deploy your solution. For this deploy your solution into the farm. Open your web.config file in (C:\inetpub\wwwroot\wss\VirtualDirectories\youwebapp\web.config).

Search after "<SafeControls>" and check if your dll is listed there and if so check if all parameters like PublicKeyToken, NameSpace or DLLName are correct

Safe Control Entry in Web.Config

Tip: If oyu deployed your assembly into the GAC, you can check the "PublicKeyToken" in your GAC. Just press "Win + R" key and enter "assembly". Search your dll and check the properties with a right click.

2. Check the package in Visual Studio

In a second step you can check the package which will be deployed with the wsp. So open your Visual Studio and click on "Package". The package manifest will appear.

Packaging XML

Check in the manifest XML if your custom dll is listed correctly. You can overwrite this generated xml manually but I don't recommend that. Mostly there is an option somewhere which generates the stuff.

But if you don't trust Visual Studio here the steps to overwrite the XML manually. First click on edit options in the Package view. Then click on the link "Overwrite generated XML and edit manifest in the XML editor".

Scenario 1:

Now you found out that your Safe Control exists but the namespace is wrong. To adjust the setting just click on your module to display the options window. Now edit the "Safe Control Entries" option:

After editing your entries you can check your manifest if your safe control entry is now updated.

Scenario 2:

You can't find your dll because your WebPart is referencing an other dll which is not automatically added to Safe Controls.

You can also add other dlls as Safe Controls. Just open the packaging explorer again and click on advanced at the bottom:

Now you can add any assembly as safecontrol into your package.


1 comment:

Harry singh said...

Hi Baris,

Even i got this error ,i will try this one. Thanks for giving the solution .

Sharepoint Developers