0
votes

I have built an empty SharePoint project in Visual Studio 2010 with some custom web parts in it. When I've tested these on my local development machine they can be added to a page without any trouble.

When I deploy the wsp via PowerShell to a remote SharePoint server, the web parts show in the web part gallery but adding one to a page gets me the error 'Cannot import this web part'. The logs have no further information in them. As per some answers I've found via Google, I've checked there is no code in Init. All initial processing is done within CreateChildControls (although I would have thought if this were the cause, I would get the same error on my local machine).

I originally asked this question at https://sharepoint.stackexchange.com/questions/11820/custom-web-part-deployment-produces-cannot-import-this-webpart-error. From the suggestions I received I decided to create a fresh project where I had done minimal coding.

The new project has just one simple label web part. The code for the web part is

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace TestWebParts.TestHelloWorld
{
    [ToolboxItemAttribute(false)]
    public class TestHelloWorld : WebPart
    {
        protected override void CreateChildControls()
        {
            Label lblThis = new Label();
            lblThis.Text = "Testing web part";
            Controls.Add(lblThis);
        }
    }
}

The .webpart file in the project is

<?xml version="1.0" encoding="utf-8"?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="TestWebParts.TestHelloWorld.TestHelloWorld, $SharePoint.Project.AssemblyFullName$" />
      <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">TestHelloWorld</property>
        <property name="Description" type="string">My WebPart</property>
      </properties>
    </data>
  </webPart>
</webParts>

I have checked that a safe control entry has been added. I have added CAS policies to the project (as these will be deployed to the bin rather than the GAC). The package manifest looks like this

<?xml version="1.0" encoding="utf-8"?>
<Solution xmlns="http://schemas.microsoft.com/sharepoint/">
  <CodeAccessSecurity>
    <PolicyItem>
      <PermissionSet class="NamedPermissionSet" version="1" Description="Permission set for ECB">
        <IPermission class="AspNetHostingPermission" version="1" Level="Minimal" />
        <IPermission class="SecurityPermission" version="1" Flags="Execution,ControlPrincipal,ControlAppDomain,ControlDomainPolicy,ControlEvidence,ControlThread" />
        <IPermission class="Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
    version="1" ObjectModel="True" />
        <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="UserName" />
        <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" PathDiscovery="*AllFiles*" />
        <IPermission class="System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" PathDiscovery="*AllFiles*" />
      </PermissionSet>
      <Assemblies>
        <Assembly Name="$SharePoint.Project.AssemblyName$" Vers7ion="$SharePoint.Project.AssemblyVersion$" PublicKeyBlob="$SharePoint.Project.AssemblyPublicKeyBlob$"/>
      </Assemblies>
    </PolicyItem>
  </CodeAccessSecurity>
</Solution>

The .wsp installs fine on the dev machine I am trying to get this running on. When I try to view the web part in the Web Part Gallery I get the message "Cannot import this web part" as shown above. I have confirmed that the .dll is in the bin directory. The web.config file holds this entry

          <SafeControl Assembly="TestWebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8d63a878e7c304c2" Namespace="TestWebParts.TestHelloWorld" TypeName="*" Safe="True" SafeAgainstScript="True" />

When I view the webpart xml from the Web Part Gallery I see

<?xml version="1.0" encoding="utf-8"?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="TestWebParts.TestHelloWorld.TestHelloWorld, TestWebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8d63a878e7c304c2" />
      <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">TestHelloWorld</property>
        <property name="Description" type="string">My WebPart</property>
      </properties>
    </data>
  </webPart>
</webParts>

Does anybody have any ideas as to what could be causing the issue when I deploy to the dev machine? The only other thing I can think of is that my local machine is Windows 7 and the dev machine is Windows Server 2008. Both are running SharePoint Foundation

1

1 Answers

0
votes

It can not find the assembly referenced in the web part. The error you are seeing is actually the equivelant from this line of code.

Cannot import this Web Part.

What you need to do is verify the full class and assembly reference names in your .webpart definition file and update accordingly.

Unfortunately I can't tell you what it should be exactly because your project definition may be outputting the assembly with different info than your class defintion.