Troubleshooting: Cannot import Web Part Error

Posted Monday, October 6, 2008 5:20 PM by CoreyRoth

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

# re: Troubleshooting: Cannot import Web Part Error

Tuesday, October 7, 2008 9:26 AM by TonyKilhoffer

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

# re: Troubleshooting: Cannot import Web Part Error

Tuesday, October 7, 2008 1:29 PM by CoreyRoth

Thanks!  That Tony guy is pretty cool.

# re: Troubleshooting: Cannot import Web Part Error

Monday, December 7, 2009 6:19 PM by Salahuddin Belal

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

# re: Troubleshooting: Cannot import Web Part Error

Tuesday, January 12, 2010 4:31 PM by Pascal

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.

# re: Troubleshooting: Cannot import Web Part Error

Tuesday, April 27, 2010 10:40 AM by Jon

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?

# re: Troubleshooting: Cannot import Web Part Error

Friday, June 25, 2010 3:00 AM by Alex

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

# re: Troubleshooting: Cannot import Web Part Error

Monday, June 28, 2010 5:07 PM by CoreyRoth

Excellent.  Glad I could help. :-)

# re: Troubleshooting: Cannot import Web Part Error

Friday, February 25, 2011 1:56 AM by Woder

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?

# re: Troubleshooting: Cannot import Web Part Error

Thursday, June 16, 2011 1:26 PM by Jeff Pearce

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."

# re: Troubleshooting: Cannot import Web Part Error

Monday, June 20, 2011 4:08 PM by CoreyRoth

@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.

# re: Troubleshooting: Cannot import Web Part Error

Tuesday, July 19, 2011 5:56 AM by Pieter van Huizen

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'.

# re: Troubleshooting: Cannot import Web Part Error

Wednesday, July 20, 2011 4:00 PM by Jim Morrison

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

# re: Troubleshooting: Cannot import Web Part Error

Tuesday, August 9, 2011 4:01 PM by Nacho

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....

# re: Troubleshooting: Cannot import Web Part Error

Wednesday, November 9, 2011 8:32 AM by Gareth

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

# re: Troubleshooting: Cannot import Web Part Error

Friday, December 9, 2011 10:37 AM by Mital

Thanks Corey this Artical helped me a lot

# re: Troubleshooting: Cannot import Web Part Error

Tuesday, January 10, 2012 8:46 AM by Paul

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

# SharePoint: Cannot import Web Part error « samiv2

Tuesday, November 20, 2012 4:49 AM by SharePoint: Cannot import Web Part error « samiv2

Pingback from  SharePoint: Cannot import Web Part error « samiv2

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

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

Leave a Comment

(required)
(required)
(optional)
(required)