Namespace not updated in SharePoint SPIs
Firstly what is a SharePoint SPI?
No it’s not another new version, it’s a generic term that is used to describe a SharePoint Project Item within the context of a Visual Studio project.
Visual Studio 2010 has introduced a lot of great SharePoint specific features making it significantly easier to get started with SharePoint development. This post however is not about the tools, it is intended to point out one little bit of the hidden functionality that unfortunately didn’t get completed in time.
The Visual Studio tools hide away so of the configuration options in .spdata files. This is normally great as you can change these via the UI tools and everything works. However I discovered a small issue during a recent refactoring of a project.
When you create a new SharePoint SPI (for example a web part) that needs to add a reference to the assembly it updates the .spdata file with information (as below)
<?xml version="1.0" encoding="utf-8"?>
<ProjectItem Type="Microsoft.VisualStudio.SharePoint.WebPart"
DefaultFile="MyWebpart.cs"
SupportedTrustLevels="All"
SupportedDeploymentScopes="Site"
xmlns="http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel">
<Files>
<ProjectItemFile Source="Elements.xml" Target="MyWebpart\" Type="ElementManifest" />
<ProjectItemFile Source="MyWebpart.webpart" Target="MyWebpart\" Type="ElementFile" />
</Files>
<SafeControls>
<SafeControl Name="MyWebpart"
Assembly="$SharePoint.Project.AssemblyFullName$"
Namespace="_21apps.Sample.SPSolution" TypeName="*" IsSafe="true" />
</SafeControls>
</ProjectItem>
In here you will see references to the ProjectItemFiles (.weppart and element manifest) and also an entry for the SafeControls.
If you look closely at the SafeControl entry you will see the Assembly is using a Token
Assembly=”$SharePoint.Project.AssemblyFullName$”
This is great as it allows the SafeControl to pickup the actual assembly name at compile time. The problem comes with the next part of the entry
Namespace=”_21apps.Sample.SPSolution”
As you can see this is not token based, which means that if you later decide to rename you namespace, and this is case sensitive, the SharePoint safe control entry will no longer be valid and you will get an error like the one below when you deploy the solution.
A Web Part or Web Form Control on this Web Part Page cannot be displayed or imported because it is not registered on this site as safe.
For more info on what this actually means have a look at Maurice Prather’s post from back in 2005, the information is still applicable today.
Unfortunately this namespace is unlikely to be made into a Token any time soon. Perhaps one for the community to pickup in the meantime. For now it is case of being aware that you need to correct this manually if you do change your namespace.
