SharePoint Online: Remove Web Part from Page using PowerShell

Requirement: Remove a web part from a page in SharePoint Online using PowerShell.

How to Delete a Web Part from a SharePoint Online Page?

Web parts are the widgets to show information, add business functionality, implement the process, act as an interface to databases or provide any other application-specific functionality. Web parts on a SharePoint Online page can be removed if they are no longer needed. You can remove a web part by:

  1. Navigate to the page where the web part is placed >> Click on Edit Page from the settings menu.
  2. Select the Delete option from the web part drop-down menu and confirm the prompt.
  3. This removes the web part from the page.

PowerShell to Delete a web part from a Page in SharePoint Online:

We have a script editor web part, titled “Google Analytics Script” on the SharePoint Online page and got a requirement to remove it using PowerShell.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#function to remove a web part from SharePoint Online page
Function Remove-SPOWebpartFromPage([String]$SiteURL,[String]$PageRelativeURL,[String]$WebPartTitle, [PSCredential]$Cred)
{ 
     Try
     {
        #Setup Credentials to connect
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
        #Set up the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
  
        #Get the web parts from the given web page
        $Web = $Ctx.web
        $Ctx.Load($Web)
        $Page= $Web.GetFileByServerRelativeUrl($PageRelativeURL)
        $WPM = $Page.GetLimitedWebPartManager("Shared")
        $Ctx.Load($WPM)
        $WebParts = $WPM.WebParts
        $Ctx.Load($WebParts)
        $Ctx.ExecuteQuery()
        write-host "Loaded web parts..."

        Write-host "Searching Web parts..."
        #Get the Web part ID to remove
        $WebPartID=[string]::Empty
        ForEach($WebPart in $WebParts)
        {
            #Get Web part properties
            $Ctx.Load($Webpart.WebPart.Properties)
            $Ctx.ExecuteQuery()            
            If($Webpart.WebPart.Properties.FieldValues["Title"] -eq $WebPartTitle)
            {
                $WebPartID = $Webpart.ID
            }
        }

        If($WebPartID -ne [string]::Empty)
        {
            $WebpartToRemove=$WPM.WebParts.GetById($WebPartID)
            $WebpartToRemove.DeleteWebPart()
            $Ctx.ExecuteQuery()
            write-host -f Green "Web Part '$($WebpartTitle)' has been removed!"
        }
        else
        {
            write-host -f Yellow "Web Part '$($WebpartTitle)' not found!"
        }        
   }
    Catch [System.Exception]
    {
        Write-Host -f Red $_.Exception.Message
    } 
} 

#Config parameters
$SiteURL = "https://crescent.sharepoint.com/"
$PageRelativeURL="/Sites/Marketing/SitePages/Home.aspx"
$WebPartTitle="Google Analytics Script"

#Get credentials to connect to 
$Credentials = Get-Credential

#Call the function to remove the web part from page
Remove-SPOWebpartFromPage -SiteURL $SiteURL -PageRelativeURL $PageRelativeURL -WebPartTitle $WebpartTitle -Cred $Credentials

Delete Web Part using Web Part ID:
The above script deletes the web part from the page from the web part title. However, If you know the web part ID, you can remove the web part simply by:

$WebPartID="a4877b9b-24b6-4a8d-8da1-95c36454fd5c"
$Webpart=$wpm.WebParts.GetById($webPartID)
$Webpart.DeleteWebPart()

To remove a web part from a modern SharePoint Online page, use: How to Remove a Web Part from a Modern SharePoint Online Page using PowerShell?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *