Not Registered as Safe, Indeed

The Perils of Renaming Your SharePoint 2010 Visual Web Part

The development experience for SharePoint 2010 is a huge step up from the elaborate Virtual Machine dance steps required by SharePoint 2007. Yet, as with any toolset this author has encountered, there remain a few hidden snares in the works.

One such snare presents itself when you have deployed a custom Visual Web Part created in Visual Studio 2010. All appears to be peachy as you move to add your new web part to a page on your site. Then, suddenly:

A Web Part or Web Form Control on this Page cannot be displayed or imported. The type could not be found or it is not registered as safe.

A quick Internet-based examination of the issue brings you to many a well-meaning post, generally aimed at SharePoint administrators, identifying the <SafeControls> section of the site’s web.config file as the scene of the crime. While this is technically true, there is no direct access to the destination site’s web.config file via a Visual Web Part project. As developers, we want this to be resolved in the code, not merely on the instance of deployment, lest we realize a slightly nauseating dystopian future where IT folk have to remember to update the config file every time your web part is deployed to a new environment.

You Missed A Spot

Thankfully, the problem is in your code. Well, not your code, per se, but rather the code generated by Visual Studio. Meticulous developer that you are, I’ll bet that you didn’t just leave the web part with its default moniker of VisualWebPart1. You right-clicked on it and gave it a meaningful name, so that the next code-smith to rummage through your work would speak of you with notes of awe quavering in their voice in some otherwise sleepy meeting yet to come.

Seriously, though, nice work. Meaningful names are an important aspect of great code. You simply did not go far enough in your nomenclatorial exploits. Each SharePoint Module generated in Visual Studio (including those that define Visual Web Parts) contains a hidden *.spdata file which enumerates (among other things) declarations of controls to be appended to the web.config’s safe list when the module is deployed. The problem indicated by our error du jour is that, while Visual Studio will automatically add a safe declaration for your custom web part in this file, it will not update the file when you rename the web part. So your SharePoint site is left saying “well, if I see this VisualWebPart1 dude, I’ll let him in, but Aptera.SharePoint.BrilliantlyNamedWebPart just isn’t on the list.”

As If You Aren’t Already Way Ahead of Me

You can remedy this (and any other renaming remnants) with a thorough search and replace of your code in Visual Studio. The relevant section of your web part’s spdata file should read:

    <SafeControl Name="SafeControlEntry1" Assembly="$SharePoint.Project.AssemblyFullName$" Namespace="Aptera.SharePoint.BrilliantlyNamedWebPart" TypeName="*" IsSafe="true" IsSafeAgainstScript="false" />

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 )

Google photo

You are commenting using your Google 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