11

I keep getting this error when I try to add a web part that I added through a WSP:

A Web Part or Web Form Control on this Page cannot be displayed or imported. The type is not registered as safe.

I installed this through the Management Shell:

Install-SPSolution -Identity mySolution.wsp -WebApplication http://sp-dev:10000 -GACDeployment

Is there something I need to do differently with my WSP in order to get this error message to go away? I would prefer a solution that allows me to solve this through my WSP if possible.

UPDATE:

Ok, I marked the additional assembly as a Safe Control and now my manifest looks like this. I noticed that the Safe Controls node under my third party assembly (DocX.dll) is the same as my Safe Control node under my main project (except for namespace). Is this correct?

  <Assemblies>
    <Assembly Location="DocX.dll" DeploymentTarget="GlobalAssemblyCache">
      <SafeControls>
        <SafeControl Assembly="MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=161g332b2e4d265e" Namespace="Novacode" TypeName="*" />
      </SafeControls>
    </Assembly>
    <Assembly Location="MyProject.dll" DeploymentTarget="GlobalAssemblyCache">
      <SafeControls>
        <SafeControl Assembly="MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=161g332b2e4d265e" Namespace="MyProject.MyProject" TypeName="*" />
      </SafeControls>
    </Assembly>
  </Assemblies>
3
  • Is there a Strong Name Key (.snk) in your project that is being used to sign your assemblies? Commented Apr 20, 2011 at 4:19
  • Yes, I have that already. Commented Apr 20, 2011 at 5:05
  • it is cause usually by renaming a webpart and its classes by hand. add a clean Visual Webpart and copy paste the main ascx and codes
    – Iman
    Commented Jul 2, 2014 at 4:37

4 Answers 4

10

Sounds like you are missing the <SafeControl> element from your package. Normally it is added for you if you create it in VS2010. But if your web part/control is in another assembly you may have to add it manualy

http://msdn.microsoft.com/en-us/library/ms412965.aspx

Something like this should go in your Package.Template.xml file (Solution explorer -> Package -> Open -> Manifest -> Edit Options)

    <Assemblies>
      <Assembly
        DeploymentTarget="GlobalAssemblyCache"
        Location="Text">
        <SafeControls>
           <SafeControl
             Assembly = "Text"
             Namespace = "Text"
             Safe = "TRUE" | "FALSE"
             TypeName = "Text"/>
        </SafeControls>
      </Assembly>
    </Assemblies>

Can you tell us a little about what controls/webparts are in the wsp?

Example default package preview has this:

<Solution xmlns="http://schemas.microsoft.com/sharepoint/" SolutionId="7905031d-87a1-47b3-b631-c93daff7e4c7" SharePointProductVersion="14.0">
  <Assemblies>
    <Assembly Location="SharePointProject1.dll" DeploymentTarget="GlobalAssemblyCache">
      <SafeControls>
        <SafeControl Assembly="$SharePoint.Project.AssemblyFullName$" Namespace="SharePointProject1.WebPart1" TypeName="*" />
      </SafeControls>
    </Assembly>
  </Assemblies>
  <FeatureManifests>
    <FeatureManifest Location="SharePointProject1_Feature1\Feature.xml" />
  </FeatureManifests>
</Solution>
4
  • There is only one visual web part in the WSP, but it IS using a third party assembly for some functionality. I added that to my "Additional Assemblies" and I notice that it has "No" set under Safe controls. Could this be the problem? Commented Apr 20, 2011 at 4:57
  • 1
    if the visual webpart uses controls in the third party assembly, then yes. Remember that the Assembly property needs to be the FullName (including the version etc)
    – djeeg
    Commented Apr 20, 2011 at 5:16
  • 1
    Awesome, take a look at my update. Does that look correct? Commented Apr 20, 2011 at 5:31
  • the first Assembly should be the FullName of DocX.dll
    – djeeg
    Commented Apr 20, 2011 at 7:06
10

Visual Studio has a bug where changing the namespace of your class does not update the manifest file of the installation package. Then when you install the safe control and execute your webpart, it is missing due to a namespace mismatch.

For example:

<SafeControl Assembly="Sharepoint_Native_Indicators, Version=1.0.0.0,
                       Culture=neutral, PublicKeyToken=cd7250552f7daf2e"
             Namespace="XXXXX.YYYYYY" TypeName="*" />
4

Dont forget to check the SharePointProjectItem.spdata file too. Most of them miss this. The namespace here may not get updated if you changed it in just the code files.

0

The problem in my scenario was the web part scope is defined for one web application where i was trying to add the web part in a completely different web application. So changing the scope of the web part to All Web applications solved the issue. Hope This Helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.