Iterating Through All WebParts On Page
One of my friends instant messaged me this morning and asked if it was feasible to use a method that would take an SPWeb object, and a URL as a parameter in order to return a collection of all the WebParts that coordinated with the URL. I haven’t tested this method, but this is what I put together for him:
- private static List<WebPart> GenerateWebPartCollection(SPWeb web, string url)
- {
- // Create A New SPLiimitedWebPartManager object to perform some WebPart operations on the page
- SPLimitedWebPartManager splwManager = web.GetLimitedWebPartManager(url, PersonalizationScope.Shared);
- // Create a new generic collection that represents all the WebParts we found
- List<WebPart> wpCollection = new List<WebPart>();
- // Loop through all the WebParts that are available
- foreach (WebPart webpart in splwManager.WebParts)
- {
- // if the generic collection does not contains that WebPart, add it
- if (wpCollection.Contains(webpart))
- {
- wpCollection.Add(webpart);
- }
- }
- // return the generic collection of WebParts to get consumed later
- return wpCollection;
- }
Its a pretty simple method, the return should just be a colleciton of the WebPart objects that can be used later for whatever his application was attempting to do.
Articles & Research
SharePoint Security
SharePoint Development
SharePoint Architecture
Claims Authentication
Forefront For SharePoint
AIS / Dynamics GP
Team Foundation Server
Pex And Moles
ISA/TMG/IAG/UAG
DPM
Cardspace
Research Methodology
Rural ICT Development
Numerical Analysis
Multi-Level Research
Knowledge Management
Personal/Off-Topic