The following are some of the common web part errors that I just wanted to summarize and provide the general solution.
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.
A Web Part or Web Form Control on this Page cannot be displayed or imported. The type DateTimeWebPart.DateTimeWebPart.DateTimeWebPart, DateTimeWebPart, Version=188.8.131.52, Culture=neutral, PublicKeyToken=2bfd9e5fd3b67b15 could not be found or it is not registered as safe.
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
(hidden – must ‘show all files’ at the Visual Studio project level)
Right Click <WebPart> Project item > Properties > Click Safe Control Entries
(IIS virtual directory, e.g. C:\inetpub\wwwroot\wss\VirtualDirectories\80)
The safe control entry is automated in deployment declared in the .spdata file.
<SafeControl Assembly=”DateTimeWebPart, Version=184.108.40.206, Culture=neutral, PublicKeyToken=2bfd9e5fd3b67b15″ Namespace=”DateTimeWebPart.DateTimeWebPart” TypeName=”*” Safe=”True” />
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.
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.
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.