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=""  
  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  
  9. Connect to SharePoint Online site  
  10. Connect-PnPOnline –Url $siteURL –Credentials (Get-Credential)  
  12. # Get the list view webpart XML   
  13. $xml=Get-PnPWebPartXml -ServerRelativePageUrl $listViewURL -Identity $wpTitle  
  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  



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