5

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?

5 Answers 5

1

I was going through a similar issue yesterday. The deployment and adding webpart worked locally on my vm (when deployed from VS), but on a dev server just gave a plain error "Cannot import webpart". This is what I finally figured:

  1. Ensure your feature actually contains the correct version of webpart/assembly.
  2. Ensure safecontrol is correctly defined with correct version
  3. Now, Delete all your solution related webparts from the webpart gallery. (This is what really helped me). You can use a ps script for that.
  4. Redeploy solution. (This will activate the features, but not add the webparts to webpart gallery).
  5. Deactivate the webpart feature either via UI or Disable-SPFeature command (i used ps)
  6. Activate the webpart feature (Enable-SPFeature). This will bring all the new updated webparts to the web part gallery.

The above is especially useful if you change your classname, namespace or versions. Now I was able to import the webpart fine. I also found this method is consistent. So I added the Delete webparts and Disable, Enable features as part of my deployment script.

9
  • Steps 1 & 2 - yes they look right to me. See stackoverflow.com/questions/5951149/… for details of same issue with code for a fresh solution. Steps 3 to 6 - I have been doing this as part of my deployment with no effect.
    – grimorde
    May 11, 2011 at 7:30
  • in the webpart gallery, do the webparts reflect the latest time (modified date/time) ?
    – vasya10
    May 12, 2011 at 3:30
  • Yes the modified date/time matches
    – grimorde
    May 12, 2011 at 8:06
  • To add it to your feature or package, VS uses another file called SharePointProjectItem.spdata. This will be like: <ProjectItemFile Source="Elements.xml" Target="MyWebPart\" Type="ElementManifest" /> <ProjectItemFile Source="MyWebPart.webpart" Target="MyWebPart\" Type="ElementFile" /> check if that file has correct references.
    – vasya10
    May 13, 2011 at 14:50
  • You said safecontrol tag is in manifest, but is it in the web.config also ?
    – vasya10
    May 13, 2011 at 14:59
0

Could you post some code as well?

Theres alot of reasons for this to fail.

One is malformed webpart xml, another is that you forgot to declare your webpart class as public, a third is you forgot to declare a safecontrol entry for your class.

EDIT after code update: Are you using VS2010's web part template? This should automatically add <SafeControl..> entries to your package (solution). to set it up manually go to the webpart element SPI and add an entry to Safe Control Entries. If you open your Package and select Manifest, safecontrol entries should be shown here.

10
  • I am using the VS2010 web part template (non visual). I added the Safe Control Entries myself when I built the web parts and they are displaying in the manifest.
    – grimorde
    Apr 26, 2011 at 12:40
  • Manifest looks like this:- <SafeControls> <SafeControl Assembly="MyWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f2ad7ede851e92d0" Namespace="MyWebPart.HelloWorld" TypeName="*" /> </SafeControls>
    – grimorde
    Apr 26, 2011 at 12:49
  • 2
    well that explains it then: take a look at your provisioned webpart: MyWebPart.UI.WebParts.HelloWorld.HelloWorld, ECB, Version=1.0.0.9506, Culture=neutral, PublicKeyToken=2986f17b005315a6 that doesnt match your safe control entry! :-) Apr 26, 2011 at 13:12
  • Ah, sorry - I've just realised I added the manifest entry for a second project I've created from scratch to see if I did something wrong when setting up - confused the issue. The manifest in my actual project is ' <SafeControl Assembly="MyWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2986f17b005315a6" Namespace="MyWebPart.UI.WebParts.HelloWorld" TypeName="*" SafeAgainstScript="True" /> '
    – grimorde
    Apr 26, 2011 at 13:42
  • Anders is correct as it appears that your versions do not match. your Dll is "1.0.0.9506" but in SafeControls you have "1.0.0.0". It also looks like your namespace differs between the two. The full DLL name (all parts) must match exactly between the DLL and the SafeControls entry.
    – Dave Wise
    Apr 26, 2011 at 14:36
0

Do you have an asterisk in the AssemblyVersion or AssemblyFileVersion values in your AssemblyInfo.cs file? SharePoint doesn't seem to like it when your version numbers changes between deployments.

Do this:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Not this:
[assembly: AssemblyVersion("1.0.0.")]
[assembly: AssemblyFileVersion("1.0.0.
")]

1
  • On my original project - yes we are using NAnt to automate builds which causes the version number to change each time. However, the test project I've created, which has the same issues, has the assembly version and assembly file version set as you have suggested.
    – grimorde
    May 10, 2011 at 13:24
0

I'd just like to thank you all for the help you supplied on the above issue.

We arranged to have the SharePoint site rebuilt. Once done, the web parts installed and imported to pages without any trouble.

Something obviously went awry with the initial installation but our sys admin guys don't know what it was.

0

Today i also faced the similar issue when i tried to import the webpart from Dev site and export to QA site. when i try to add this webpart to the page , it was giving the error."$Resources:core,ImportErrorMessage" . I could not figure out the solution for hours. I came to this stack Exchange Questions and did almost everything which has said here in other answers. like Adding the Safe control to Web config file and checking the public Key token and match with the assembly available in Webpart. but i was getting the Error again and again.

By doing try and trial once i Deployed the solution in Dev using VS 2010. and instead of Importing the .Webpart file from webpart gallery and exporting to the QA webpart gallery, i got the .webpart file from the "pkg" folder which is created by VS 2010 and then uploaded to QA webpart gallery and suddenly Error gone. you can try this if above answer does not work . it might help.

for your reference if you want to look http://www.spissue.com/2015/01/getting-resourcescoreimporterrormessage.html

Please let me know if anybody having objection or correction to my answer. since it was hit and trial but it worked for me so thought to share here.

Thanks.

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.