Remove a Web Part from All Pages in SharePoint Site Collection using PowerShell
Requirement:
After the SharePoint migration from SharePoint 2013 to SharePoint 2016, decided to remove a broken web part from all pages wherever its being used.
How to Remove a Web Part from Page in SharePoint?
To remove a web part from page, you can edit the page and simply delete the web part. In case of page crash, use web part maintenance mode by appending "?contents=1" to the URL and then remove the unwanted web part. E.g. http://intranet.crescent.com/SitePages/Home.aspx?contents=1
PowerShell to Remove a Web Part from All Pages in a Site Collection:
Instead of deleting a web part from each single page from browser, we can leverage PowerShell to remove a particular web part from all pages in a site collection.
After the SharePoint migration from SharePoint 2013 to SharePoint 2016, decided to remove a broken web part from all pages wherever its being used.
How to Remove a Web Part from Page in SharePoint?
To remove a web part from page, you can edit the page and simply delete the web part. In case of page crash, use web part maintenance mode by appending "?contents=1" to the URL and then remove the unwanted web part. E.g. http://intranet.crescent.com/SitePages/Home.aspx?contents=1
PowerShell to Remove a Web Part from All Pages in a Site Collection:
Instead of deleting a web part from each single page from browser, we can leverage PowerShell to remove a particular web part from all pages in a site collection.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue #Configuration parameters $SiteURL = "http://intranet.crescent.com" $WebPartTypeName="*BambooCalendar*" #Get All Subsites in a site collection and iterate through each $Site = Get-SPSite $SiteURL ForEach($Web in $Site.AllWebs) { Write-host Processing $Web.URL # If the Current Web is Publishing Web if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web)) { #Get the Publishing Web $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web) #Get the Pages Library $PagesLib = $PubWeb.PagesList #Pages Library } else { $PagesLib = $Web.Lists["Site Pages"] } #Iterate through all Pages foreach ($Page in $PagesLib.Items | Where-Object {$_.Name -match ".aspx"}) { $WebPartsToDelete = @() $PageURL=$web.Url+"/"+$Page.File.URL write-host $pageurl $WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) #Get All Web Parts of given type foreach ($WebPart in $WebPartManager.WebParts | Where-Object { $_.GetType().ToString() -like $WebPartTypeName} ) { write-host "Found the Web Part at "$PageURL -f Green $WebPartsToDelete += $WebPart.ID } #If the web part to delete is found If($WebPartsToDelete) { #Checkout if required if ($Page.File.RequiresCheckout) { if ($Page.File.CheckOutStatus -ne "None") { write-host "Overriding Checkout..." $Page.File.UndoCheckOut() } write-host "Checking Out..." $Page.File.CheckOut() } #Remove the web part from page foreach ($WebPart in $WebPartsToDelete) { Write-Host "Deleting Web Part on $($web.Url)/$($page.Url)" $WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) $WebPartManager.DeleteWebPart($WebPartManager.webParts[$webPart]) } if ($Page.File.CheckOutStatus -ne "None") { Write-host "CheckIn-In..." $Page.File.CheckIn("Deleted web part") $Page.File.Publish("") } if ($Page.ParentList.EnableModeration) { write-host "Approving..." $Page.File.Approve("") } } } }This script iterates through all sites and pages in the entire site collection and removes the given web part.
Remove a Web Part from All Pages in SharePoint Site Collection using PowerShell
Reviewed by Salaudeen Rajack
on
January 18, 2016
Rating:

No comments:
Please Login and comment to get your questions answered!