Thursday, January 18, 2007 9:00 AM Jan Tielens

How to get a List of Web Parts on a Page

Recently I got a question from a student how to get a list of all the web parts (in code) that are displayed on a specific SharePoint Web Part Page. This could be useful if you would like to close all the web parts at once, add web parts programmatically etc. By using the SharePoint object model this is pretty easy, especially if you want to retrieve that list with code running in a web part. The WebPartManager property of the WebPart base class, gives you access to a collection of all the WebPartZones available on the page. Once you've got the zones, you can get the web part instances by using the WebParts property. The following web part code will display a small list of all the web parts found on the page, including the name of the web part class and the web parts zone.

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;

namespace WebPartLister
{
  public class WebPartLister : System.Web.UI.WebControls.WebParts.WebPart
  {
    BulletedList list;

    protected override void CreateChildControls()
    {
      list = new BulletedList();
      WebPartZoneCollection zones = this.WebPartManager.Zones;
      foreach (WebPartZone zone in zones)
      {
        WebPartCollection webparts = zone.WebParts;
        foreach (WebPart webpart in webparts)
        {
          list.Items.Add(
          string.Format("{0} ({1}), {2}",
            webpart.Title, webpart.GetType().Name,
            zone.DisplayTitle));
        }
      }
      this.Controls.Add(list);
    }

    protected override void Render(HtmlTextWriter writer)
    {
      EnsureChildControls();
      list.RenderControl(writer);
    }
  }
}

 

Technorati tags: ,
Filed under:

Comments

# re: How to get a List of Web Parts on a Page

Thursday, January 18, 2007 5:33 AM by Stefaan Rillaert

Hello Jan,

a small remark : you can skip overriding the Render() method. WebPart inherits from System.Web.UI.Control which will by default render its childcontrols. So you only need to add the BulletedList to the childcontrols in CreateChildControls(), which you already do.

Have fun coding ! Stefaan

# re: How to get a List of Web Parts on a Page

Monday, January 22, 2007 5:34 AM by Jan Tielens

Hi Stefaan, you are absolutely right! I'm so used to building web parts with a more complex UI so I implement the Render method myself all the time by default... :-)

# re: How to get a List of Web Parts on a Page

Monday, January 22, 2007 2:42 PM by Robert Bogue

This will miss any web parts that are currently in an error state.  :)

# re: How to get a List of Web Parts on a Page

Tuesday, March 06, 2007 12:14 AM by Anil Kumar

I am new sharepoint and i would like to have a resource discovery where in i can read all the webpages  and webparts name with hierarchy

# re: How to get a List of Web Parts on a Page

Tuesday, March 20, 2007 9:05 AM by Ravi Kumar Neelam

Hi Jan,

Its very usefull to me

thanks you very much Jan

# re: How to get a List of Web Parts on a Page

Friday, April 06, 2007 11:44 AM by W.R. Matthiesen

I am adding WebParts to a sharepoint page that is not the page I am on.  (AddWebPart(newWebPart,"Left",0) Nobody seems to indicate how to determine where to get the zones for a different page.  The only thing I have seen is via the WebPartManager, but I can find no way to access a WebPartManager for a page that is not instantiated.  Thanks

# re: How to get a List of Web Parts on a Page

Wednesday, April 25, 2007 9:22 AM by Sam

I want to try to accomplish this idea in SharePoint 2007, but in a different way.  I have an SPWeb object and need to troll through all the SPFiles and SPDocumentLibrary's in the web to find active and erroneous web parts.  This is done on site creation from a custom site template (.stp file).

Without using obsolete/depricated methods, is this possible?

# re: How to get a List of Web Parts on a Page

Friday, May 11, 2007 3:56 PM by Louvaro

Is it possible to know the existing web part zones available in the all the page layout's in a sharepoint 2007 site?

# re: How to get a List of Web Parts on a Page

Wednesday, July 11, 2007 2:27 AM by Raja

How can i get the list of webparts in site?

# re: How to get a List of Web Parts on a Page

Wednesday, August 15, 2007 10:07 PM by milly

hey hey whats up dawg