Wednesday, February 8, 2012 15:04

The type is not registered as safe

Tagged with:
Posted by on Monday, November 30, 2009, 23:10
This news item was posted in SharePoint category and has 0 Comments so far.

“A Web Part or Web Form Control on this Page cannot be displayed or imported. The type is not registered as safe.”
I came across such error when I adding a web part into web part page. What I did was just compiling the project with the latest framework my sharepoint using. Of course, I googled this error before I fixed this error, one solution is provided below. It could also solve my problem although i didn’t try it.

The type is not registered as safe

Figure 1. To fix the error – The type is not registered as safe.

Some experts from MSDN:

Specifying Safe Web Parts
Allowing users or even members of the Administrator site group to have unrestricted freedom in importing new Web Parts can expose a server to security threats. For this reason, Web Parts must be explicitly designated as safe before they become available on the virtual server. This is done by making entries in the SafeControls section of a Web.config file for the virtual server. The Web.config file is typically found at this location:

system_drive:\Inetpub\wwwroot\Web.config
Each SafeControls entry identifies an assembly that contains one or more Web Parts. You can list Web Part classes individually or you can specify that all Web Parts in the assembly are safe. Here is an example of the SafeControls section from a Web.config file, with tags preceding it that show how it fits into the XML hierarchy of the file:

<configuration>
  <SharePoint>
    <SafeControls>
      <SafeControl Assembly="System.Web, Version=1.0.5000.0,
       Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
       Namespace="System.Web.UI.WebControls" TypeName="*" Safe="True" />
      <SafeControl Assembly="System.Web, Version=1.0.5000.0,
       Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
       Namespace="System.Web.UI.HtmlControls" TypeName="*" Safe="True" />
      <SafeControl Assembly="Microsoft.SharePoint, Version=11.0.0.0,
       Culture=neutral, PublicKeyToken=71e9bce111e9429c"
       Namespace="Microsoft.SharePoint" TypeName="*" Safe="True" />
      <SafeControl Assembly="Microsoft.SharePoint, Version=11.0.0.0,
       Culture=neutral, PublicKeyToken=71e9bce111e9429c"
       Namespace="Microsoft.SharePoint.WebPartPages" TypeName="*"
       Safe="True" />
      <SafeControl Assembly="Northwind, Version=1.0.0.0,
       Culture=neutral, PublicKeyToken=null" Namespace="Northwind"
       TypeName="*" Safe="True" />
    </SafeControls>

Each SafeControl element has four attributes:

  • Assembly The name of an assembly that contains one or more Web Parts. For assemblies that are strong named, you must include the name, version, culture and public key token. For other assemblies, only the name is required, although all four parts may be included.
    CAUTION If you give a strong name to the sample Northwind assembly by adding the path to a key pair file in the Assemblyinfo.cs file for the project, you must add the public key token to the SafeControl entry for that assembly. An easy way to get a strong-named assembly’s public key token is to add the assembly to the GAC by dragging the .dll file into the local_drive:\Windows\Assembly directory. You can then right-click the assembly and click Properties to see the properties of the assembly. The public key token is displayed, and this view of the token is handy because you can select it and copy it to the clipboard. If you want to remove the assembly from the GAC, right-click the assembly, and then click Delete.
  • Namespace The .NET namespace for the Web Part class. Note that Web Parts in nested namespaces must be listed separately, even if an asterisk is entered for TypeName. For example, the SafeControls XML section shown earlier includes two separate entries for the assembly Microsoft.SharePoint—one for the Microsoft.SharePoint namespace and one for the Microsoft.SharePoint.WebPartPages namespace.
  • TypeName The class name of the Web Part. You can use an asterisk (*) to indicate that an entry applies to all Web Part classes in the specified assembly and namespace. This is especially handy when you are developing the assembly, because no changes are required as you add new Web Part classes and recompile your assembly.
  • Safe This attribute usually has a value of True. However, members of the Administrator site group can deny the safety of a Web Part and make it unavailable by setting this attribute to False.

The SafeControls listing prevents rogue Web Parts from becoming available to users. Additionally, Web Parts are subject to the same Code Access Security controls that are applied to all managed code.

Leave a Reply

You can leave a response, or trackback from your own site.