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.