Cannot import web part, solution

Posted: 22nd May 2012 by Gennady in C#, SharePoint
Tags: ,

k20120523113719

 

Solution: move code from webpart constructor to OnInit method of the Page.

Error: this error appears when adding webpart to Page. Moreover, sometimes this error could be reproducted only on target server, but in Dev environment all could be ok.

After log analysis(by example using ULSViewer) I found this error message, that doesn’t say much:

Error importing WebPart. Assembly  ***, Version=1.0.0.0, Culture=neutral, PublicKeyToken=***, TypeName. ***.***

In my case the reason was in constructor code:

public MyWebpart(){

this.ExportMode = WebPartExportMode.All;

// get context
web = SPContext.Current.Web;
}

When adding webpart, WebPartImporter calls webpart constructor, that does not work properly. Solution is – to move code from constructor to OnInit method of the Page. After this webpart adds to page successfully.

 

 

Click below to expand comments form..