11
votes

I have a SharePoint web part (essentially just a "Hello World" app) that I just created and am having a problem deploying it. I have signed the .dll, created the .dwp, and registered it as a safe control in web.config. I am able to add it to the Web Part Gallery and add the details for it; however, when I attempt to add it to a page, I get the following error:

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.

Following is my .dwp file:

<?xml version="1.0"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
   <Assembly>SimpleWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=################</Assembly>
   <TypeName>MyWebParts.SimpleWebPart</TypeName>
   <Title>My Simple Web Part</Title>
   <Description>A simple Web Part</Description>
</WebPart>

and the entry I added to web.config:

<SafeControl Assembly="SimpleWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=################" Namespace="MyWebParts" TypeName="*" Safe="True" />

I also tried using wildcards for the namespace, which didn't help. I have even tried setting the web.config trust level to "Full" (which I would never do in production, but tried to attempt to narrow down the problem) and still had no luck. Any ideas? Thanks.

10
Uh, this might be a stupid question, but do you have the actual public key token in your files in lieu of a bunch of hashes? ...It's called "public" for a reason.Preston Guillot
That's not a stupid question at all. I do have the actual key in place of those hashes.Geo Ego

10 Answers

33
votes

One possibility is that types names are not in sync with the .webpart file. For ex.

In the .webpart file:

<?xml version="1.0" encoding="utf-8"?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="Namespace.Class1, $SharePoint.Project.AssemblyFullName$" />

and in the .cs file:

namespace Namespace
{
    public class Class2 : WebPart

and in the SharePointProjectItem.spdata file:

  <SafeControl Assembly="Class2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9385058ce1ee51a9" Namespace="Namespace" TypeName="*" Safe="True" SafeAgainstScript="False" />

you get the idea: triple-check names consistency across all project artifacts.

4
votes

Are you deploying your webpart using a SharePoint Solution (.wsp file)? Check out WSPBuilder if you aren't. We also use SharePoint Installer Between the two, a lot of the problems (whether stemming from human error or otherwise) in deployments, like your problem have been resolved in our environment.

3
votes

Make sure that you have the Web Part class as Public, might sound silly but I faced once. Also try to populate the web part from the Web part Gallery .

3
votes

I also saw this problem when I changed the namespace for my WebPart assembly, and the namespace wasn't updated in all of the source files in the solution.

2
votes

Did you try deploying it to the GAC?

1
votes

Ok , this may be very late but will be useful for the rest. I created a webpart and was getting the error "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."

I figured that I was using a Chart inside this webpart and once I removed the reference of the chart, it worked. So, the Chart control has to be also marked as typesafe.

Make sure all the references in the webpart are type safe.

1
votes

This usually happens when you change the name of the web part from VisualWebPart1 to MyNewWebPart. After doing a Search and Replace over the entire solution, rename all of the files and folders with VisualWebPart1 to MyNewWebPart. The problem is caused by the file SharePointProjectItem.spdata not being updated. Open SharePointProjectItem.spdata and replace VisualWebPart1 with MyNewWebPart. This fixes the problem in most cases.

Open every file in the solution and verify that there are no references to VisualWebPart1. If there are, change them manually to MyNewWebPart.

0
votes

Geo,

It's been a while since I worked with Web Parts but I laid out the steps here:

http://www.codersbarn.com/?tag=/webpart

Maybe there's something there that can help.

Anthony :-)

0
votes

As Tim Scarborough already mentioned, this can happen if you change the namespace and don´t update all source files (as this is not done automatically). Just for illustration, in my case the problem was that the webpart class had a new namespace a.b but I forgot to update this in the webpart file:

<?xml version="1.0" encoding="utf-8"?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
             //update below!
  <type name="a.b.yourClass, $SharePoint.Project.AssemblyFullName$" />

  <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
</metaData>
<data>
 <properties>
    <property name="Title" type="string">your Title</property>
    <property name="Description" type="string">your Description</property>
  </properties>
</data>
 </webPart>
</webParts>
0
votes

Well, if you have already found the solution, that's great. But here is a tip for someone who is searching for more. If you added a new webpart or a visual webpart and just replace the namespace in the files such as code files or Element.xml or even .webpart file you might still face this issue. The reason is, as mentioned somewhere above, the namespace was not changed in .spdata file, which is a file created by Visual studio itself. Just to be sure go check in the Virtual Directories in the inetroot. If the namespace is still the older one, do a search in your Solution and find out the conflicting one. Most probably in the project.spdata file, which everyone tends to ignore.