Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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:

share|improve this answer

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

share|improve this answer
    
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. –  Abe Miessler May 10 '11 at 16:28
2  
Should this be a comment? –  Abe Miessler May 10 '11 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 '11 at 16:53

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...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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