Add, Remove and Get all webparts from modern site page using PnP PowerShell

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

1. First We Need to Connect to Site. To Perform Connection Add

$siteURL = Read-Host "Provide site url" 
                
       Connect-PnPOnline -Url $siteURL 
             #Executing this line will ask for credentials. Provide use name and password to connect.

$page = Get-PnPClientSidePage -Identity "ModernWebPage.aspx"    #Get the page on which you are going to perform add and remove web parts.

2. Add different webparts.

  • To add a text editor web part
Add-PnPClientSideText -Page $page -Text "Welcomes To SharePoint"
  • To add a list view web part
Add-PnPClientSideWebPart -Page $page -DefaultWebPartType “List” -WebPartProperties @{selectedListId="609f95e4-7022-417d-a57f-693673f7eff9"} 

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

3. Retrieve all web parts used in modern site pages.

$webParts = $page.Controls

    # if there are more than one webparts  
   foreach($webpart in $webparts){  
       
        Write-Host "WebPart Id " $webpart.InstanceId  
  
        Write-Host "Title " $webpart.Title    
}  

4. Remove a webpart

    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 web parts. After executing the above command, it will open a dialog box to confirm web part delete. Click on yes to delete web part.

$siteURL = Read-Host "Please provide site url"

try
{

    Connect-PnPOnline -Url $siteURL
    $page = Get-PnPClientSidePage -Identity "ModernWebPage.aspx"
    
# Add webparts in modern page

  
Add-PnPClientSideText -Page $page -Text "Welcomes To SharePoint" -Section 2 -Column 1 

Add-PnPClientSideWebPart -Page $page -DefaultWebPartType “List” -Section 2 -Column 1 -WebPartProperties @{selectedListId="609f95e4-7022-417d-a57f-693673f7eff9"}

 # Retrieve webparts from  modern page

$webParts = $page.Controls

   foreach($webpart in $webparts){  
       
        Write-Host "WebPart Id " $webpart.InstanceId  
  
        Write-Host "Title " $webpart.Title    
}


# Remove webparts from  modern page

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

catch
{
    Write-Host -ForegroundColor Red 'Error ', ':' $Error[0].ToString();
    sleep 10
} 

This solution is brought to you by our SharePoint professionals.

Softree Consulting employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Softree Consulting is an award-winning SharePoint consultancy that has been working with clients from across the globe to transform workplaces with tools and platforms from Microsoft technology. As a widely recognized organization, we are commonly approached to incorporate technical excellence and innovative design into all our customer actions.....

Site Footer