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

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
               }
          }
     }
}
Bookmark and Share
Leave a comment

0 Comments.

Leave a Reply


[ Ctrl + Enter ]

Performance Optimization WordPress Plugins by W3 EDGE