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.