in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint, Office 365, Mobile Development, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

Troubleshooting: Cannot import Web Part Error

Let's face it, any new SharePoint developer is going to run into an error such as the one below when starting out.

[WebPartPageUserException: Cannot import MyWebPart Web Part.]

  at Microsoft.SharePoint.WebPartPages.WebPartImporter.CreateWebPart(Boolean clearConnections)

  at Microsoft.SharePoint.WebPartPages.WebPartImporter.Import(SPWebPartManager manager, XmlReader reader, Boolean clearConnections, Uri webPartPageUri, SPWeb spWeb)

  at Microsoft.SharePoint.WebPartPages.SPWebPartManager.CompressWebPartNoSave(Boolean isClosed)

I've found myself doing a LOT of troubleshooting of this for developers, so I decided it was time to post the steps in helping me resolve this issue.  This issue can be a number of things, but the first place I always check is the web part gallery.  If you can't view your web part in the gallery, chances are it's not going to work.  The first thing you need to do is verify that your web part is in fact in the list.  If it's not, you need to go install it (either manually or via solution package / feature).  If it is there, click on the link to the web part (i.e. MyWebPart.webpart), not the icon.  If you get an error like the one above, you definitely know something is wrong with the way the web part is installed on your server.

The next thing you do is click the Edit icon (the one next to the webpart link).  From this page, you can make use of the View XML link to look at the .webpart file that is installed.  More than likely at this point, the assembly path is wrong to your DLL or the DLL is not installed on your system.  Confirm that the assembly path is correct given your DLL.  For example, if your DLL is strongly signed, make sure the public key token is listed on the assembly path.  If the DLL is not in the GAC, I recommend leaving off the PublicKeyToken becuase it will try and find that DLL in the global assembly cache which will give you an error saying that it cannot find the DLL.  Other issues I have ran into here is the version number.  Sometimes specify the version number as 1.0.* (for example).  This usually works but I have seen issues where it would not work unless you specified 1.0.0.0.  I am sure there is a reason for that, but I have no idea what it is.  Also remember, if you change your assembly path ever (i.e.: you change the PublicKeyToken or namespace) and redeploy, you need to delete the .webpart file out of the gallery.  You can do this manually or write code to do it with a feature receiver.

If you have verified that your .webpart is correct, make sure that the DLL for you web part actually exists.  I know this sounds obvious, but lots of things can cause a solution package to install silently which means your DLL may not be present in the bin folder or the GAC when you think it should be.  Also remember that you probably need to reset IIS (or at least the application pool), if you have deployed a DLL to the GAC for SharePoint to pick it up.

Of course, make sure you have a SafeControls entry in the web.config file for your assembly.  Although, this would probably result in a Cannot register this type as safe error.  This error can often times be quite frustrating because it will seem as if you have done all the steps to make sure it works and it just doesn't.  Don't forget to check the logs (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\logs) too.  This error will show up in there and there may be extra information in there to help you find your error.  Just keep at it and double check everything and usually you can find something.

Comments

 

TonyKilhoffer said:

Trust in Corey to keep the best sharepoint tips coming. Thanks, Corey!

October 7, 2008 9:26 AM
 

CoreyRoth said:

Thanks!  That Tony guy is pretty cool.

October 7, 2008 1:29 PM
 

Salahuddin Belal said:

Thanks! I was struggling for hours trying to resolve the webpart issue.

December 7, 2009 6:19 PM
 

Pascal said:

I would like to update an existing installation with same assemblies but with different public key. Is there a way to have existing web part on my pages to point to the "renamed" assemblies. I was thinking at writing a program to export web parts for each page, changing assembly reference then reimporting them. However this will create a new web part instance (GUID) on my page and blow up any web part connection. Any hint would be helpful.

January 12, 2010 4:31 PM
 

Jon said:

I'm getting the same error when trying to use Microsoft.SharePoint.Portal.WebControls.TagCloudWebPart

It works fine if i do it via the web ui, but fails when using the ONET.XML for my sites.

Any ideas?

April 27, 2010 10:40 AM
 

Alex said:

Thanks Corey, really good article and quite useful. You show me the light XD

June 25, 2010 3:00 AM
 

CoreyRoth said:

Excellent.  Glad I could help. :-)

June 28, 2010 5:07 PM
 

Woder said:

Thanks for your explanation Corey but I'm getting this error and I've check twice all the steps. Does someone know another issue regarding WebPartImporter?

February 25, 2011 1:56 AM
 

Jeff Pearce said:

How about explaining how to fix a Web Part that is listed in the gallary but when you click on the text to edit it, you get a "Cannot import this Web Part."

June 16, 2011 1:26 PM
 

CoreyRoth said:

@Jeff It's actually the same issue.  It means it cannot find the web part given the specified assembly path.  Instead of clicking on the link to the web part, click the icon next to it so that you can view properties, review the .webpart file and make corrections is necessary.

June 20, 2011 4:08 PM
 

Pieter van Huizen said:

I have found an extra check for when the 'could not import web part' error occurs.

I did have the same problems as mentioned in your post, so I looked for all mentioned possible mistakes but could not find the solution. In the end it turned out I forgot to make the (persisted) properties of my web part 'Public'.

July 19, 2011 5:56 AM
 

Jim Morrison said:

Another famous trick is to omit  "public" from your webpart class declaration. Happens on Monday sometimes.

July 20, 2011 4:00 PM
 

Nacho said:

In my case I was recieving this error because I have an exception thrown inside the webpart code... althougt it was being handle by a try catch block....

August 9, 2011 4:01 PM
 

Gareth said:

Check the case all matches of your namespace and class too!

November 9, 2011 8:32 AM
 

Mital said:

Thanks Corey this Artical helped me a lot

December 9, 2011 10:37 AM
 

Paul said:

You also get this error if your .webpart file attempts to set a property that does not exist in the webpart class.

January 10, 2012 8:46 AM
 

SharePoint: Cannot import Web Part error « samiv2 said:

Pingback from  SharePoint: Cannot import Web Part error « samiv2

November 20, 2012 4:49 AM
 

Debugging feature activation in SharePoint 2007 | C Language Development | C Programming Language Tutorial said:

Pingback from  Debugging feature activation in SharePoint 2007 | C Language Development | C Programming Language Tutorial

May 2, 2013 9:14 PM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2015 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems