So it’s often the case that I start a project and then decided later I want to refactor such elements like the namespace, package name etc. Let’s start with the namespace.
Project Namespace
When you create a new project (lets start with an empty solution), you provide the project name, solution name, and location.
Project: SharePointProject1
Solution: SharePointSolution1

Great. We have our solution. We add a web part item to the project and we get:

Before we go any further, lets build and deploy. Everything looks good according to the web part preview from the web part gallery.

Now we want to change the following:
- Solution Name
- Namespace
- Project Name
- Assembly Name
Renaming the solution, project and files in Solution explorer, and the assembly name through project properties should not cause a problem. But when we change the namespace in the web part code (and update the web part file), we get the following error:

This typically indicates a problem between the web part file, the assembly, and the SafeControl entry in web.config.
If we look at the deployed web part file we see:
.webpart
- <?xml version=“1.0“ encoding=“utf-8“?>
- <webParts>
- <webPart xmlns=“http://schemas.microsoft.com/WebPart/v3“>
- <metaData>
- <type name=“contoso.webparts.SampleWebPart, contoso.webparts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b896b8322cc3e164“ />
- <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
- </metaData>
- <data>
- <properties>
- <property name=“Title“ type=“string“>WebPart1</property>
- <property name=“Description“ type=“string“>My WebPart</property>
- </properties>
- </data>
- </webPart>
- </webParts>
If we look at the web.config for the safe control entry:
Web.Config
- <SafeControl Assembly=“contoso.webparts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b896b8322cc3e164“ Namespace=“SharePointProject1.WebPart1“ TypeName=“*“ Safe=“True“ SafeAgainstScript=“False“ />
Notice the SafeControl entry is incorrect for Namespace. In order to correct this, the file SharePointProjectItem.spdata (located in the web part folder in windows explorer) needs to be updated. SharePoint does not update this file for you.
SharePointProjectItem.spdata
- <?xml version=“1.0“ encoding=“utf-8“?>
- <ProjectItem Type=“Microsoft.VisualStudio.SharePoint.WebPart“ DefaultFile=“SampleWebPart.cs“ SupportedTrustLevels=“All“ SupportedDeploymentScopes=“Site“ xmlns=“http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel“>
- <Files>
- <ProjectItemFile Source=“Elements.xml“ Target=“SampleWebPart\\cf0 ” Type=“ElementManifest“ />
- <ProjectItemFile Source=“SampleWebPart.webpart“ Target=“SampleWebPart\\cf0 ” Type=“ElementFile“ />
- </Files>
- <SafeControls>
- <SafeControl Name=“SafeControlEntry1“ Assembly=“$SharePoint.Project.AssemblyFullName$“ Namespace=“SharePointProject1.WebPart1“ TypeName=“*“ IsSafe=“true“ IsSafeAgainstScript=“false“ />
- </SafeControls>
- </ProjectItem>
Notice the SafeControl entry above where the namespace value is incorrect. Change this to the correct namespace and re-deploy. Now we’re back in business!