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).
Does anybody have any suggestions as to what else could be causing this error?
Updated - code added as requested
Code:-
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 MyWebPart.UI.WebParts.HelloWorld
{
[ToolboxItemAttribute(false)]
public class HelloWorld : WebPart
{
protected override void CreateChildControls()
{
Label lblThis = new Label();
lblThis.Text = "Hello World!";
Controls.Add(lblThis);
}
}
}
From the .webpart in Visual Studio:-
<?xml version="1.0" encoding="utf-8"?>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="MyWebPart.UI.WebParts.HelloWorld.HelloWorld, $SharePoint.Project.AssemblyFullName$" />
<importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">HelloWorld</property>
<property name="Description" type="string">My WebPart</property>
</properties>
</data>
</webPart>
</webParts>
and from the .webpart taken when it has been deployed to SharePoint on the remote server:-
<?xml version="1.0" encoding="utf-8"?>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="MyWebPart.UI.WebParts.HelloWorld.HelloWorld, ECB, Version=1.0.0.9506, Culture=neutral, PublicKeyToken=2986f17b005315a6" />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">HelloWorld</property>
<property name="Description" type="string">My WebPart</property>
</properties>
</data>
</webPart>
</webParts>
I did think it may be a problem with the type name but the above files show them to match.
The deployment is done to the bin directory, rather than to the GAC, and the CAS policies I have set up in the project are:-
<?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$" Version="$SharePoint.Project.AssemblyVersion$" PublicKeyBlob="$SharePoint.Project.AssemblyPublicKeyBlob$"/>
<Assembly Name="Components" />
</Assemblies>
</PolicyItem>
</CodeAccessSecurity>
</Solution>
Updated
Following on from the help I've received from Anders below, I've created a new Empty SharePoint Project in Visual Studio 2010 called MyWebParts. To this I have added a web part called HelloWorld (simple label). I have added no CAS policies to this project as I didn't think it would be necessary for a label display web part. Examining the manifest in VS shows the Safe Control has been added automatically
<SafeControls>
<SafeControl Assembly="MyWebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f2ad7ede851e92d0" Namespace="MyWebParts.HelloWorld" TypeName="*" SafeAgainstScript="True" />
</SafeControls>
When I examine the .webpart file from SharePoint I see
<type name="MyWebParts.HelloWorld.HelloWorld, MyWebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f2ad7ede851e92d0" />
I can deploy this web part via Powershell commands to my local machine and display the web part fine. If I deploy to the dev machine I get the same error message as before 'Cannot import this web part'.
The only difference I can now think of is environment. My local machine is SharePoint 2010 Foundation installed on a Windows 7 machine. The dev machine is SharePoint 2010 Foundation installed on a Windows 2008 R2 server.
Any thoughts?