Home > Development, SharePoint > Renaming your SharePoint web part namespace

Renaming your SharePoint web part namespace

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

image

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

image

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

image

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:

image

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
  1. <?xml version=1.0 encoding=utf-8?>
  2. <webParts>
  3. <webPart xmlns=http://schemas.microsoft.com/WebPart/v3>
  4. <metaData>
  5. <type name=contoso.webparts.SampleWebPart, contoso.webparts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b896b8322cc3e164 />
  6. <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
  7. </metaData>
  8. <data>
  9. <properties>
  10. <property name=Title type=string>WebPart1</property>
  11. <property name=Description type=string>My WebPart</property>
  12. </properties>
  13. </data>
  14. </webPart>
  15. </webParts>

If we look at the web.config for the safe control entry:

Web.Config
  1. <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
  1. <?xml version=1.0 encoding=utf-8?>
  2. <ProjectItem Type=Microsoft.VisualStudio.SharePoint.WebPart DefaultFile=SampleWebPart.cs SupportedTrustLevels=All SupportedDeploymentScopes=Site xmlns=http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel>
  3. <Files>
  4. <ProjectItemFile Source=Elements.xml Target=SampleWebPart\\cf0 ” Type=ElementManifest />
  5. <ProjectItemFile Source=SampleWebPart.webpart Target=SampleWebPart\\cf0 ” Type=ElementFile />
  6. </Files>
  7. <SafeControls>
  8. <SafeControl Name=SafeControlEntry1 Assembly=$SharePoint.Project.AssemblyFullName$ Namespace=SharePointProject1.WebPart1 TypeName=* IsSafe=true IsSafeAgainstScript=false />
  9. </SafeControls>
  10. </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!

Advertisements
  1. Somnath
    July 26, 2011 at 3:07 am

    What do you mean by “the file SharePointProjectItem.spdata (located in the web part folder in windows explorer)”? Can you please provide exact location.

    • July 28, 2011 at 6:03 pm

      Right click the solution and select ‘open in explorer’. Then look for a folder that mathches your web part. In there you will find the spdata file.

  1. December 4, 2010 at 9:02 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: