Following code snippet can be used to remove a web part from an existing SharePoint website:
- using (SPSite spSiteTest = new SPSite(“SiteURL”)
- {
- using (SPWeb spWebTest = spSiteTest.OpenWeb())
- {
- SPWebPartCollection webparts = spWebTest.GetWebPartCollection("WebPageURL", Storage.Shared);
- for (int k = 0; k < webparts.Count; k++)
- {
- //get reference to webpart
- Microsoft.SharePoint.WebPartPages.WebPart wp = webparts[k];
- //check webpart Title to find webpart which is to be removed
- if (wp.Title == "WebPartName")
- {
- //delete webpart
- webparts.Delete(wp.StorageKey);
- //update spWeb object
- spWebTest.Update();
- } }
- }
- }
No comments:
Post a Comment