Wednesday, December 16, 2009

ErrorWebPart when loading a CQWP using the object model

When you are using the object model from a console or windows application to load web parts, you will notice that the CQWP (content query web part) instances all show up as "ErrorWebPart", and do not allow changing any of the web part's settings.

This is because when you load the web part, the web part code tries to access all kinds of data from it's context. Since you are loading the web part without a context (that is, from a windows application instead of a web application) the web part fails to get the context, and gets reported as an ErrorWebPart, when infact it works well if you look at it on the web interface.

I have yet to find a solution for this, but one workaround that works is to move your code to a web page, in the "_layouts" folder. This allows the code to run withing the site's context, and so accessing the web part does not cause it to report ErrorWebPart.

Update: It has been brought to my attention by a reader that Sandeep has a workaround for this:

1 comment:

Anonymous said...