Showing posts with label Mircosoft. Show all posts
Showing posts with label Mircosoft. Show all posts

October 28, 2021

How to get list of web parts from Modern SharePoint Page?

Introduction:

In this blog, we will learn how can we get web parts added to the Modern Sitepage.
On the classic page, we can get a list of all web parts using "?contents=1" query string, but it will not work for modern web parts. For the modern web part, we need to use the PowerShell script.

Script:

Step 1: First we will read the URL of the site of which page we want to get the web parts.
 $siteURL = Read-Host "Provide site url";  
  • Here we are taking user input for site URL, we can also give static Site URL.

Step 2: Now we will connect to the site using the Connect-PnPOnline command. 
 Connect-PnPOnline -Url $siteURL  
  • This command will ask for credentials.

Step 3: Now we will get the page using "Get-PnPClientSidePage" command.
 $page = Get-PnPClientSidePage -Identity 'Home'  
Here we are using the Home page, we can use the name of any of your page.

Step 4: In $page will have all the information for the Home page. So now we will get all web parts available on this page as below:
 $webParts = $page.Controls   

Step 5: Now $webParts will have a list of all web parts available on the Home page. So we will use a loop to read each web part information:
 foreach($webpart in $webparts) {   
   Write-Host "WebPart Id "   
   $webpart.InstanceId   
   Write-Host "Title "   
   $webpart.Title   
 }  

Here we are fetching the web part ID and web part name. 

Step 6: Complete PowerShell script will be as below:
 $siteURL = Read-Host "Provide site url"   
 Connect-PnPOnline -Url $siteURL  
 $page = Get-PnPClientSidePage -Identity 'Home' #Get the page on which you are going to perform add and remove web parts.  
 $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   
 }  
This PowerShell will give output as below screenshot:



It will return Custom as well as OOTB web parts.

Conclusion:

This is how we can get a list of web parts. Hope this helps!

If you have any questions you can reach out our SharePoint Consulting team here.