Monday, January 18, 2016

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.

PowerShell Script to

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.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...