kwizcom banner advertisement

SharePoint Online: Remove Web Part from Page using PowerShell

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

PowerShell to Delete a web part from a Page in SharePoint Online:
We have a script editor web part, titled "Google Analytics Script" in 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=""
        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 "")
        {
            $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 page from the web part title. However, If you know the web part ID, you can remove web part simply by:
$WebPartID="a4877b9b-24b6-4a8d-8da1-95c36454fd5c"
$Webpart=$wpm.WebParts.GetById($webPartID)
$Webpart.DeleteWebPart()
SharePoint Online: Remove Web Part from Page using PowerShell SharePoint Online: Remove Web Part from Page using PowerShell Reviewed by Salaudeen Rajack on May 28, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.