mylifeinaminute.com

Locating all of the Web Parts on the Current Page (C#)

by liquidpooled on Nov.13, 2008, under .NET, MSDN, Microsoft, Office, Sharepoint Server, TechNet, Visual Studio 2005, Visual Studio 2008, Windows SharePoint Services

The following snippet allows for locating all of the web parts on a given web part page (the sample is targeted at web parts of the type Microsoft.SharePoint.WebPartPages.ListViewWebPart)


string webUrl = SPContext.Current.Web.Url;

using (SPSite site = new SPSite(webUrl))
{
     using (SPWeb web = site.OpenWeb())
     {
          SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(this.Parent.Page.Request.Url.ToString(), System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

          SPLimitedWebPartCollection webPartColl = manager.WebParts;

          for (int i = 0; i < webPartColl.Count; i++)
          {
               if (webPartColl[i].GetType() == typeof(Microsoft.SharePoint.WebPartPages.ListViewWebPart))
               {
                    // do stuff
               }
          }
     }
}
No comments for this entry yet...

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...