Saturday, July 10, 2010

Deleting webparts programmatically in a page, MOSS 2007 (Sharepoint)

Simple way to remove webpart in a page programmatically. [Here I am trying to remove all webparts which are added to 'dafault.aspx' page when I created to Report site]
[SPLimitedWebPartManager  is under "microsoft.sharepoint.webpartpages" namespace]

SPLimitedWebPartManager reportWebPartManager = sourceWeb.GetLimitedWebPartManager("Pages/default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

int webPartCount = reportWebPartManager.WebParts.Count;
for (int i = webPartCount - 1; i >= 0; i--)
{
reportWebPartManager.DeleteWebPart(reportWebPartManager.WebParts[i]);
}
reportWebPartManager.Dispose();

No comments:

Post a Comment