Add, Remove, And Get All Web Parts From Modern Site Page Using PnP PowerShell

In this blog, we are going to retrieve all the webparts present in a modern site page. Also, we will see how to add different web parts and delete a web part from a modern site page using PnP PowerShell.

First, we need to connect to the site. To perform the connection, add the following lines.

  1. $siteURL = Read-Host "Provide site url"  
  2. Connect-PnPOnline -Url $siteURL  
  3. #Executing this line will ask for credentials. Provide use name and password to connect.  
Add, Remove And Get All WebParts From Modern Site Page Using PnP PowerShell
  1. $page=Get-PnPClientSidePage Identity "ModernWebPage.aspx" #Get the page on which you are going to perform add and remove web parts.  

Add different Webparts

To add a text editor web part,
  1. Add-PnPClientSideText -Page $page -Text "Welcomes To SharePoint"  
To add a list view web part,
  1. Add-PnPClientSideWebPart -Page $page -DefaultWebPartType “List” -WebPartProperties @{selectedListId="609f95e4-7022-417d-a57f-693673f7eff9"}  
Add, Remove And Get All WebParts From Modern Site Page Using PnP PowerShell

You can see two web parts are added successfully in the modern site page. 

Add, Remove And Get All WebParts From Modern Site Page Using PnP PowerShell

Retrieve all web parts used in the modern site pages.

  1. $webParts = $page.Controls  
  2. #if there are more than one webparts  
  3. foreach($webpart in $webparts) {  
  4.     Write - Host "WebPart Id "  
  5.     $webpart.InstanceId  
  6.     Write - Host "Title "  
  7.     $webpart.Title  
  8. }  
Add, Remove And Get All WebParts From Modern Site Page Using PnP PowerShell

Remove a webpart

  1. Remove-PnPClientSideComponent -Page $page -InstanceId 0c6a1475-c5e8-414e-9e6d-c90c3b5c01b7   

To remove web parts, we need to provide web part instance id that we have already got in PowerShell window output while retrieving the web parts. After executing the above command, it will open a dialog box to confirm web part delete. Click on "yes" to delete the web part.

Add, Remove And Get All WebParts From Modern Site Page Using PnP PowerShell
  1. $siteURL = Read - Host "Please provide site url"  
  2. try {  
  3.     Connect - PnPOnline - Url $siteURL  
  4.     $page = Get - PnPClientSidePage - Identity "ModernWebPage.aspx"  
  5.     #Add webparts in modern page  
  6.     Add - PnPClientSideText - Page $page - Text "Welcomes To SharePoint" - Section 2 - Column 1  
  7.     Add - PnPClientSideWebPart - Page $page - DefaultWebPartType“ List” - Section 2 - Column 1 - WebPartProperties @ {  
  8.         selectedListId = "609f95e4-7022-417d-a57f-693673f7eff9"  
  9.     }  
  10.     #Retrieve webparts from modern page  
  11.     $webParts = $page.Controls  
  12.     foreach($webpart in $webparts) {  
  13.         Write - Host "WebPart Id "  
  14.         $webpart.InstanceId  
  15.         Write - Host "Title "  
  16.         $webpart.Title  
  17.     }  
  18.     #Remove webparts from modern page  
  19.     Remove - PnPClientSideComponent - Page $page - InstanceId 0 c6a1475 - c5e8 - 414e-9 e6d - c90c3b5c01b7  
  20. catch {  
  21.     Write - Host - ForegroundColor Red 'Error '':'  
  22.     $Error[0].ToString();  
  23.     sleep 10  
  24. }