3

Is there anyway to use the object model to display a list of all web parts installed (say in a list box)? If so would it then be possible to render that web part on a page if it were selected?

3 Answers 3

5

You can enumerate /_catalogs/wp files, this way (code to run in your own webpart):

var webPartManager = WebPartManager.GetCurrentWebPartManager(Page);
foreach (SPFile file in web.GetFolder("/_catalogs/wp").Files)
{
    WebPart webPart = webPartManager.ImportWebPart(file.Url, out errorString);
    if (webPart != null)
    {
        string webPartName = webPart.GetType().Name;
        // here you should add to your listbox webPartName
        // and file.UniqueId as a value, to be able on postback
        // quickly retrieve this file, using web.GetFile
    }
}

Or you can also use file.Name as the display name, but here is also an example of using WebPartManager, which will help you to add the selected webpart to the page.

And to add the imported webpart to the page, you should use SPLimitedWebPartManager.AddWebPart method in postback (all the webpart properties will have their default values, unless you initialize them manually before adding to the page).

Additional references:

0

Do you need something other than the Web Part Gallery and it's preview functionality?

3
  • Yes, I need more than that. Actually, if you know of a way to reproduce that functionality using the object model, that would be a good start for what I need in the end. May 10, 2011 at 16:28
  • 2
    Should this be a comment? May 10, 2011 at 16:28
  • It is on the line in between. Had this been what you were looking then it would have been an answer. To more directly answer your question, listing the web parts is definitely possible via code. Showing a preview of the web part would take some digging but should be possible by leveraging the existing preview code.
    – Dave Wise
    May 10, 2011 at 16:53
0

I asked a similar question about WSS 3.0 / MOSS 2007. The answer I found should still be relevant for 2010 I think.

It will enable you to enumerate the web parts in the catalog and then instantiate each one and add it to a web part page if you wanted. Preview thumbnails would be nice...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.