Easily Adding SafeControls in SharePoint 2010 with Visual Studio 2010

Many SharePoint developers know that deploying your SharePoint customization to a SharePoint server often requires you to tinker the web.config of the sites where you’d like to see the customization in action. You can do these web.config modifications manually (not advisable), or automate them with the help of the SPWebConfigModification class from the Object Model. But quite often it’s not even necessary to use that class; when you deploy your customizations with the help of a Solution (.WSP file), like you should, the Solution deployment mechanism can modify the Web.Config to apply the most commonly used changes. This is defined in the Solution Manifest of the Solution file. The following Solution Manifest will add for example a SafeControl element for the current Assembly, which is required to display controls like Web Parts, User Controls etc. in the SharePoint sites.

<Solution xmlns="http://schemas.microsoft.com/sharepoint/" SolutionId="684047e7-a990-4917-8a84-5d2ae5c8b1f3" SharePointProductVersion="14.0">
  <Assemblies>
    <Assembly Location="SafeControlsDemo.dll" DeploymentTarget="GlobalAssemblyCache">
      <SafeControls>
        <SafeControl Assembly="$SharePoint.Project.AssemblyFullName$" Namespace="SafeControlsDemo.EmptyElement1" TypeName="*" />
      </SafeControls>
    </Assembly>
  </Assemblies>
  <FeatureManifests>
    <FeatureManifest Location="SafeControlsDemo_Feature1\Feature.xml" />
  </FeatureManifests>
</Solution>

Visual Studio 2010 will automatically add the necessary SafeControl element to the Solution manifest when you add for example a Web Part item to the Visual Studio SharePoint project. But sometimes Visual Studio is not smart enough, or doesn’t have enough information to automatically do this for you. A pretty common scenario is when you build a CustomAction using the ControlClass and ControlAssembly attributes, pointing to a User Control that should be rendering the UI element defined in the CustomAction. In this case you explicitly have to add the required SafeControl element (for the User Control) to the Solution Manifest. As you probably know you can modify the Package.Template.xml to add the required SafeControl elemen, but you have to write that XML manually. A better idea is to use the power of the Visual Studio 2010 SharePoint developer tools, available out-of-the-box! The little trick is to select the item in your SharePoint 2010 project in the Visual Studio Solution Explorer window. The Properties window will show, for the selected project item, a property called Safe Control Entries.

When you click the ellipsis (the three dots), Visual Studio will show a dialog in which you can easily add the required SafeControl entries. Visual Studio will even fill out default values for you, referencing the assembly of the current project. As a result, the Solution Manifest will be updated with the corresponding XML (you can verify that by opening the Package item and selecting the Manifest view at the bottom). Easy isn’t it?

3 Comments

  • Hi Stefan, sure it's only one small think I mentioned. But I have to work with SharePoint devs all the time and quite a lot of them are not aware of the functionality in my post. My article was not meant to give a complete overview. :-)
    Btw, your link points to an article describing tokens, my post isn't about those tokens ...

  • Yes thats another problem i have you use tokens without explicit naming them. ;-)
    For example safe control definition works better this way:




    You can use those token to name the Namespace too. You don't have to hardcode it.

  • Great article! As a sharepoint newbie, this really helped in deploying my master page, as I couldn't for the life of me get the safe control entry working. Thanks!

Comments have been disabled for this content.