Second Life of a Hungarian SharePoint Geek

March 10, 2010

Changing the namespace of the web part in a Visual Web Part project

Filed under: Deployment, SP 2010, Visual Web Part — Tags: , , — Peter Holpar @ 19:08

The default namespace for the web part class in a Visual Web Part project is YourVisualWebPartProjectName.VisualWebPart1. When you change the namespace and deploy the project you get the following exception:

UnsafeControlException: A Web Part or Web Form Control on this Page cannot be displayed or imported.

The exception is displayed on the web part page as:

Web Part Error: A Web Part or Web Form Control on this Page cannot be displayed or imported. The type {your web part class type} could not be found or it is not registered as safe.

To change the namespace that is registered in the SafeControls block, you have to open and edit a file that is hidden a bit. At least I found no direct way to set the values on the Visual Studio IDE (like project or package properties), so I searched for the original namespace in the entire project and found a file called SharePointProjectItem.spdata that contains the namespace for the SafeControl. You can even add multiple SafeControl element if you need, all of these elements will be added to the SafeControls block in the web.config file.

It is wired that after changing the namespace of the web part, when we check the preview of the package manifest (see the upper part of the Manifest tab on the Package designer) still the original namespace is displayed in the SafeControl element, but it does not affect the deployment (SafeControl registration) and working of the web part.

And do not forget to change the type of the web part (name attribute of the webParts/webPart/metaData/type element) in the .webpart file according the new namespace value!

Advertisements

4 Comments »

  1. This helped, thanks!

    Comment by Seth Hildebrand — June 11, 2010 @ 13:35

  2. This Helped, it was simple and sweet, keep the good work
    Thanks

    Comment by Prem — February 11, 2011 @ 10:14

  3. I searched for the file. But didn’t find it. Can you please provide exact location?

    Comment by Somnath — July 26, 2011 @ 08:31

    • Somnath,
      The SharePointProjectItem.spdata file shold be at the same folder where the SharePoint artifact (in this case the Visual Web Part file) exists. It is hidden by Visual Studio, but you can search for it with either Visual Studio or Windows Explorer. I usually look for the .spdata extension, because it is easier to remember for and to type than the full name.

      Comment by Peter Holpar — August 31, 2011 @ 13:04


RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: