Monday, March 21, 2011

SPLimitedWebPartManager Class in SharePoint 2010

In this post we will discuss about SPLimitedWebPartManager Class in SharePoint 2010.

Also you can check out:

- How to create a custom list definition using visual studio 2010?

- Connected web part in SharePoint 2010 using Visual WebPart in SharePoint 2010

- Create a list-based autohosted app in SharePoint 2013

SPLimitedWebPartManager Class allows developer with direct access to the collection of Web Parts on a Web Part page.

It allows to add, customize, personalize, and delete Web Part instances.
SPLimitedWebPartManager is available in SharePoint solutions deployed at the farm level, but it cannot be used in sandboxed solutions.

Here is the SharePoint 2010 object model code sample to delete all the web parts in a page

// Get Web Part Manager for home page
SPWeb site = (SPWeb)properties.Feature.Parent;
SPFile homePage = site.GetFile("default.aspx");

SPLimitedWebPartManager wpm;
wpm = homePage.GetLimitedWebPartManager(PersonalizationScope.Shared);
// delete all Web Parts on page
while (wpm.WebParts.Count > 0) {

SPFile is used to get a web part page.