Common SharePoint Web Part Errors

The following are some of the common web part errors that I just wanted to summarize and provide the general solution.

Error Message:

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.

Image

OR

A Web Part or Web Form Control on this Page cannot be displayed or imported. The type DateTimeWebPart.DateTimeWebPart.DateTimeWebPart, DateTimeWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2bfd9e5fd3b67b15 could not be found or it is not registered as safe.

OR

An error occurred when previewing the Web Part.

Ways to reproduce error

  • Preview web part by Site Settings > Web parts (Galleries) > Click web part list item; OR
  • Insert web part to a page; OR
  • View a page that previously inserted the web part.

The files in question

Image

1) .cs

Image

2) .webpart

Image

3) .spdata
(hidden – must ‘show all files’ at the Visual Studio project level)

Image

OR

Right Click <WebPart> Project item > Properties > Click Safe Control Entries

Image

4) Web.config
(IIS virtual directory, e.g. C:\inetpub\wwwroot\wss\VirtualDirectories\80)

The safe control entry is automated in deployment declared in the .spdata file.

<SafeControls>
<SafeControl Assembly=”DateTimeWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2bfd9e5fd3b67b15″ Namespace=”DateTimeWebPart.DateTimeWebPart” TypeName=”*” Safe=”True” />
</SafeControls>

5) Assembly / DLL file missing  in the Global Assembly Cache for GAC deployment (c:\windows\assembly) or virtual directory bin folder in web app deployment.

Resolution

Make sure the Namespace and Type Name are consistent across all files where indicated. Also with matching case sensitivity.

Verify web.config file and assembly in GAC or virtual directory bin folder in post deployment.

Background

What does it mean by ‘type could not be found’? What is the type?

The type in question is the class inheriting from WebPart class. In this case, it is DateTimeWebPart.DateTimeWebPart.DateTimeWebPart. Excuse my naming convention. When the .NET run time is creating an instance of the web part, the run time can not find the type; either because the assembly is missing, inaccurately referenced or misnamed.

What does it mean by ‘Is not registered as safe’?

Given that the type can be found and the type is registered in the web.config in the <SafeControls> xml node.

The safe control entries mark a web control (instantiated in the web part) such that an untrusted user has access to the web control. Or another way to look at it is that the web application trusts the web control.

3 thoughts on “Common SharePoint Web Part Errors

  1. James

    Hi I’m a bit confused. I’m having the issue above all my files are exactly like yours above. Still when I go to the web part gallery and click on the XML I get the error: “An error occurred when previewing the Web Part.” Also when I’m trying to add the webpart to the page I get an error: “An error occurred when attempting to add the item to the page”.

    Whats weird is I have the same solution deployed on one machine through visual studio, but when I copy the .wsp and deploy to another farm (also editing the web config verifying the safe controls are there) I get these errors. I have retracted the solution and deleted all traces of it on the working machine and redeployed no problems. Just on that one server I’m having problems……

    Any advice you can give on what might be causing the issue please advise.

    1. James,

      I would step back and look at for some “environmental” issue perhaps. Did you check the GAC if the deployed assembly exists (c:\windows\assembly)?

      For me the crazy issue was the case sensitivity in the code was not consistent between code files. Sorry if my reply is not that helpful. Let me know if there is a troubleshooting area I can include that I have not written about.

      Roy

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 )

Connecting to %s