How To Add List View Webpart To SharePoint Online Webpart Page Using PnP PowerShell

In this blog, you will see how to add list view webpart to SharePoint Online webpart page using PnP PowerShell.

I have a custom list named “Expense Report” and I would like to add that in the home page as list view web part.

You can download setup files from the releases section of the PnP PowerShell repository.

Copy the below script and paste it in a notepad. Save the file as AddListViewWP.ps1.

  1. # Input Parameters  
  2. $siteURL="https://c986.sharepoint.com/sites/Flow"  
  3. $wpTitle="Expense Report"  
  4. $listViewURL="/sites/flow/Lists/Expense%20Report/AllItems.aspx"  
  5. $wpPageURL="/sites/Flow/Pages/Home.aspx"  
  6. $zoneID="Header"  
  7. [int]$zoneIndex=1  
  8.   
  9. Connect to SharePoint Online site  
  10. Connect-PnPOnline –Url $siteURL –Credentials (Get-Credential)  
  11.   
  12. # Get the list view webpart XML   
  13. $xml=Get-PnPWebPartXml -ServerRelativePageUrl $listViewURL -Identity $wpTitle  
  14.   
  15. Add webpart to webpart page  
  16. Add-PnPWebPartToWebPartPage -ServerRelativePageUrl $wpPageURL -XML $xml -ZoneId $zoneID -ZoneIndex $zoneIndex  

Open PowerShell window and run the following command.

  1. >cd "<folderlocation>"  

folderlocation – AddListViewWP.ps1 file location

Run the following command,

  1. >.\AddListViewWP.ps1  

Reference

Add-PnPWebPartToWebPartPage

Thus in this blog, you saw how to add list view webpart to SharePoint Online webpart page using PnP PowerShell.