How to Remove a Web Part from Modern Page in SharePoint Online using PowerShell?
Requirement: Remove a web part from a Modern page in SharePoint Online.
How to Remove a Web Part from SharePoint Online Page?
In SharePoint Online, If you no longer need a particular web part on your page, you can remove it by editing the page and then deleting the web part. This process is relatively simple and only requires a few steps. In this blog post, we’ll show you how to remove a web part from a page in SharePoint Online using the browser, and PowerShell.
To remove a web part from the SharePoint Online page, follow these steps:
- Login to your SharePoint Online site >> Navigate to the page where you want to remove a web part.
- Click on the “Edit” button in the top-right corner of the page.
- Select the web part by placing the cursor in the web part you would like to delete >> Click on the delete button to delete the web part from the page.
- Click on the “Republish” button to save your changes.
PnP PowerShell to Remove a Web Part from SharePoint Online Page
You can also use PowerShell to remove a web part from a modern page. This can be handy if you want to remove a web part from multiple pages. Here’s how it’s done:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$PageName ="Branding-Templates.aspx"
$InstanceId = "56a09c21-3cd8-4fc8-8967-6a950f0c8738"
#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive
#Remove Web Part from page
Remove-PnPPageComponent -Page $PageName -InstanceId $InstanceId -Force
You can obtain the InstanceID using DevTools in the browser (shortcut key: F12).
You may not know the web part instance ID always, and it may not be efficient to hard-code it in the script, isn’t it? Instead, let’s locate a web part based on its type and position (section/column) and then remove it from the page:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$PageName ="Branding-Templates.aspx"
Try {
#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the page
$Page= Get-PnPPage -Identity $PageName
#Get the specific Web Part from the page
$WebParts = $page.Controls
$WebPartToRemove = $WebParts | Where {$_.Type.Name -eq "PageText" -and $_.section.Order -eq 1 -and $_.column.Order -eq 1}
If($WebPartToRemove -ne $Null)
{
#Remove the Web Part from page
Remove-PnPPageComponent -Page $Page -InstanceId $WebPartToRemove.InstanceId -Force
$Page.Publish()
Write-Host "Web Part has been Removed from the page successfully!" -f Green
}
Else
{
Write-Host "Web Part Not Found!" -f Yellow
}
}
Catch [System.Exception]
{
Write-Host -f Red "Error:"$_.Exception.Message
}
This script tries to get the Text web part in the 1st section and 1st column of the page and removes it. Please note, You may have to adjust the Web part type, section, and column parameters in the script accordingly.
Conclusion
In conclusion, removing a web part from a SharePoint Online page is a simple process that can be accomplished through the use of web browser interface or PowerShell. The PowerShell script provided in this tutorial allows users to easily remove a web part from a SharePoint Online page. By using the Connect-PnPOnline and Remove-PnPPageComponent cmdlets, users can specify the web part they wish to remove and the page it is located on, and the script will remove the web part from the page. This can be a useful tool for maintaining and updating a SharePoint Online site, as it allows users to quickly and efficiently remove unnecessary or outdated web parts.