Forum Discussion
Adding a web part with PnP PowerShell
I am trying to add a couple of list web parts to a modern team site's home page. The site is created via teams. When I visit the home page of the Team's SharePoint site, I see the defualt webparts like Quicklinks, Activity and News. However, when I connect to the site via Powershell and try to add a web part to a section, I get the error
Add-PnPClientSideWebPart : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
When I inspect the page via get-pnpclientside page, the page shows one section and one column with nothing in them. I've tried using home as well as home.aspx
$page = Get-PnPClientSidePage -Identity "home.aspx"
$page;
PageTitle : Home
Sections : {}
Controls : {}
LayoutType : Home
KeepDefaultWebParts : False
Context : OfficeDevPnP.Core.PnPClientContext
PagesLibrary : SitePages
PageListItem : Microsoft.SharePoint.Client.ListItem
DefaultSection : OfficeDevPnP.Core.Pages.CanvasSection
CommentsDisabled : False
$page.sections.count
1
When I try to add a section using add-pnpclientsidesection, the home page gets wiped of all the web parts that are there already. It seems like get-pnpclientsidepage isn't actually getting the home page but creating a new one. Is there a way to add additional web parts to the home page?