SPLimitedWebPartManager Class
Provides a limited set of Web Part operations that can be performed in object model scenarios when there is no HttpContext and no instantiated Page object.
Namespace:
Microsoft.SharePoint.WebPartPages
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
SPLimitedWebPartManager provides a subset of the operations available on the SPWebPartManager. It is primarily used for object model scenarios in which there is no HttpContext and no instantiated Page object."
GetLimitedWebPartManager() method returns a SPWeb object which needs to be disposed.
GetLimitedWebPartManager() method returns a SPWeb object which needs to be disposed.
void SPLimitedWebPartManagerNoLeak()
{
using (SPSite siteCollection = new SPSite(http://moss))
{
using (SPWeb web = siteCollection.OpenWeb())
{
SPFile page = web.GetFile("Source_Folder_Name/Source_Page");
using (SPLimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
try
{
// ...
}
finally
{
webPartManager.Web.Dispose();
}
}
} // SPWeb object web.Dispose() automatically called
void SPLimitedWebPartManagerNoLeak()
{
using (SPSite siteCollection = new SPSite(http://moss))
{
using (SPWeb web = siteCollection.OpenWeb())
{
SPFile page = web.GetFile("Source_Folder_Name/Source_Page");
using (SPLimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
try
{
// ...
}
finally
{
webPartManager.Web.Dispose();
}
}
} // SPWeb object web.Dispose() automatically called
- 2/6/2012
- IDicker-Rosedale
This is how you can find particular webpart type from the page
//Here i have to find all ContentByQuery webparts from the page
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager manager = web.GetLimitedWebPartManager("Pages/default.aspx", PersonalizationScope.Shared);
foreach (Microsoft.SharePoint.WebPartPages.WebPart webPart in manager.WebParts)
{
//<type name="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart, Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
Type type = webPart.GetType();
if (type.FullName.Contains("ContentByQueryWebPart")
{
//then it is ContentByQueryWebPart
}
}
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager manager = web.GetLimitedWebPartManager("Pages/default.aspx", PersonalizationScope.Shared);
foreach (Microsoft.SharePoint.WebPartPages.WebPart webPart in manager.WebParts)
{
//<type name="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart, Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
Type type = webPart.GetType();
if (type.FullName.Contains("ContentByQueryWebPart")
{
//then it is ContentByQueryWebPart
}
}
- 12/28/2011
- Sandip Patil
Using SPLimitedWebPartManager To Find Web Parts By Title
You can use LINQ and SPLimitedWebPartManager in order to find a WebPart object by title in the following fashion:
protected static WebPart FindWebPartByTitle(SPLimitedWebPartManager webPartManager, string title)
{
return webPartManager.WebParts.Cast<WebPart>().FirstOrDefault(webPart => Equals(webPart.Title, title));
}
Adam Buenz
SharePoint Foundation MVP - http://www.sharepointsecurity.com
- 10/14/2010
- Adam Buenz - MVP