Generic error messages… SharePoint can sometimes be full of them. This morning I ran into the “Web Part Error:: Cannot import this Web Part” error (below) that I’ve seen many times before, but none of the usual fixes corrected it.
The background story is that I’m developing a number of site templates that are pre-populated with security groups, lists, content types, and web parts for easy deployment of a custom application hosted on a SharePoint site. [As an aside, in the next week or so I’ll be posting about how to get started on setting up site templates and what tools you can use to make your life much easier].
Up until this morning there were never any problems deploying the web part in question. This morning we did a new push of our build to the test environment. Most times we completely wipe out the site collection and start from scratch (to catch errors just like this). As it turns out, this web part was only throwing an error if it was added at site creation/provisioning time. If you deleted and re-added the web part it would function as normal.
Doing some compares on the source code for the web part showed that I had recently added some security checks to ensure the user accessing the page had proper permissions (i.e. Site Readers couldn’t use an edit textbox in the web part). As I was performing this check numerous times, I decided to run the check once and store the outcome in a variable. Unfortunately I placed that security check in the Web Part constructor code (see below).
this.ExportMode = WebPartExportMode.All;// my security check was here
After a little sleuthing I moved the security check code out of the constructor and into my CreateChildControls() method.
base.CreateChildControls();// new location of security check// add controls to the web part
catch (Exception ex)
Making that little adjustment allowed the web part to be added without an error. Apparently during site creation, the account that was instantiating the site (SharePoint System or perhaps the Application Pool ID?) was throwing an exception when hitting the security check code in the constructor.
So lessons learned for the day:
- Do complete site rebuilds often to catch very rare bugs such as this
- Be aware of where you place your custom code
- SharePoint errors (can) lead to learning the intricacies of how it works as a web application
As I said, starting to prepare some material for a future blog post on how to export site data such as content types, site definitions, etc. Until then, leave feedback if you found this article helpful and remember to share your knowledge with others.
– Frog Out
2 thoughts on ““Cannot import this Web Part” Error with SharePoint Site Template”
Originally posted on: https://briantjackett.com/archive/2009/06/18/ldquocannot-import-this-web-partrdquo-error-with-sharepoint-site-template.aspx#591743This program is not very difficult. You can use the Help if you have some questons about work with it.
Originally posted on: https://briantjackett.com/archive/2009/06/18/ldquocannot-import-this-web-partrdquo-error-with-sharepoint-site-template.aspx#603971That type codding basically use creating overriding function and also exception handling.