30 January 2011

The Type could not be found or is not registered as safe

Programmer's best friend is the "copy - paste".

This is the case, I was asked to create a Webpart in SharePoint, which is similar to Webpart that I developed in the past. The way I did this was: I copied (and pasted) the project, changed the Name, the Namespace, the Assembly and of course I modified the code requested.
Everything worked fine.
I moved it to my test server and then I got 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.


This time, the programmer's best friend failed

I checked in web.config that - Webpart is defined as safe - indeed it was.
Weird ?!?!?
Again I returned to code, I checked if there is any residue of the previous Webpart code. I found that there was nothing there.
What is this? Am I rookie? It's not my first Webpart.
Then came the Enlightenment
I added to the code, ToolboxData and Guid properties, and it solved the problem.

...
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Runtime.InteropServices;
using System.Xml.Serialization;

namespace
Roi.WebParts.WebPartTest
{
    [CLSCompliant(false)]
    [Guid("9554E05E-31EF-4bcc-B0BF-0AB8CF7E5FCD")]
    [ToolboxData("<{0}:MyWebpart runat=server></{0}:MyWebpart>"),
    XmlRoot(Namespace = "Roi.WebParts.WebPartTest")]
    public class MyWebpart : WebPart
    {
...

Cheers,
Roi



3 comments:

Ryan Jauregui said...

Hey Roi, thanks for taking the time to write this up. It helped me immensely.

Tyas said...

Hi Roi, may I know which GUID that you added to code? Where can I get this GUID?

Thank you so much!

Roi kolbinger said...

hi tyas, all you need a new guid. you can use vs tool to create the new guid

Post a Comment