Using WebPartPage, WebPartManager, and WebPart
There was a small question in the SharePoint development newsgroups that alluded to whether it was possible to use the SharePoint WebPartPage and SPWebPartManager objects in order to construct a small iteration through all the WebParts that were placed within an arbitrary WebPart page in a SharePoint instance and search for a specific WebPart type. The objects that will be exposed in this code example are pretty straightforward, notably:
WebPartPage - Represents a SharePoint WebPart page
SPWebPartManager - Derived from the ASP.NET 2.0 WebPart manager, the SPWebPartManager object manages all the WebParts and WebPart zones on a page.
WebPart - Represents, well you know, a frickin WebPart on the page
In this example, I will show you how to use the aforementioned objects, and then show you to how to build the loop that was the main part of the persons query in the newsgroups.
Here is the code, in this I will test for the most standard WebPart type, a ListViewWebPart, since it is orthodox and out-of-the-box.
-
if (!string.IsNullOrEmpty(this.SPWebPartManager))
-
{
-
-
WebPartPage l_oPage = (WebPartPage) this.Page;
-
SPWebPartManager l_oManager = (SPWebPartManager) l_oPage.Master.FindControl(this.SPWebPartManager);
-
if (l_oManager.DisplayMode.Name.Equals(WebPartManager.BrowseDisplayMode.Name))
-
{
-
foreach (WebPart l_oPart in l_oManager.WebParts)
-
{
-
{
-
// Perform some action after the type test is complete, depending on your type return
-
}
-
}
-
}
-
}
4 Comments »
RSS feed for comments on this post. TrackBack URL





















Articles & Research
SharePoint Architecture
Personal/Off-Topic
Latest Free SharePoint Software
SecureCenter For SharePoint
SharePoint Security Assurance Program™
Free Online SharePoint Security Tools
Online SharePoint Security Health Assessment
Article Or Research Filed Under 
Hello!
I am trying to connect two DataFormWebPart. Any ideas?
I get SPLimitedWebPartManager, but it is not helpfull. Please, help me!
Comment by Elena — December 3, 2007 @ 11:11 am
Hi Elena,
you should do something yourself too rather than asking here and there …hopefully now you will look in your problem more seriously
Comment by smith — January 8, 2008 @ 1:25 am
smith is right in saying so…!!
Comment by angela — January 8, 2008 @ 1:28 am
big assumption there, smith
Comment by Terry — January 13, 2009 @ 1:24 pm