0

I am using PnP PowerShell to update the home page of a site. I would like two columns, one with a news webpart and the other column with an events webpart and a people webpart.

If I specify -Column 1 for all webparts, I get all webparts added to the page. But if I specify that I want the news webpart in -Column 1 and the other webparts in -Column 2, I will get an error message for the webparts that were supposed to go to the second column:

Add-PnPPageWebPart : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
+   Add-PnPPageWebPart -Page $page -Section 1 -Column 2 -Order 1 -Defau ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Add-PnPPageWebPart], ArgumentOutOfRangeException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.WebParts.AddWebPart

This is my code. What am I doing wrong?

$page = Get-PnPClientSidePage -Identity "Home"
  $page.ClearPage()

  #Add-PnPPageSection -Page "Home" -SectionTemplate OneColumnVerticalSection
  Add-PnPPageSection -Page "Home" -SectionTemplate TwoColumnLeft -Order 10
  
  Add-PnPPageWebPart -Page $page -Section 1 -Column 1 -Order 1 -DefaultWebPartType “NewsFeed” -WebPartProperties @{layoutId=”FeaturedNews”;title=”News”}
  Add-PnPPageWebPart -Page $page -Section 1 -Column 2 -Order 1 -DefaultWebPartType “Events” -WebPartProperties @{layoutId=”List”;title=”Project Milestones”}
  Add-PnPPageWebPart -Page $page -Section 1 -Column 2 -Order 2 -DefaultWebPartType “People” -WebPartProperties "{`"title`":`"Project Contacts`",`"persons`":[{`"id`":`"$projectManager`"}]}"

1 Answer 1

0

The workaround is to get rid of the original home page and start with a fresh page. It is also required to make an extra Get-PnPPage after Add-PnPPageSection, before you start adding webparts to the sections.

Remove-PnPPage -Identity "Home" -Recycle -Force

  $PageLayout = "Home"
  Add-PnPPage -Name "Home"

  $page = Get-PnPPage -Identity "Home.aspx"
 
  Add-PnPPageSection -Page "Home" -SectionTemplate OneColumnVerticalSection
  $page = Get-PnPPage -Identity "Home.aspx"
  
  Add-PnPPageWebPart -Page $page -Section 1 -Column 1 -Order 1 -DefaultWebPartType “NewsFeed” -WebPartProperties @{layoutId=”FeaturedNews”;title=”News”}
  Add-PnPPageWebPart -Page $page -Section 1 -Column 2 -Order 2 -DefaultWebPartType “Events” -WebPartProperties @{layoutId=”List”;title=”Project Milestones”}
  Add-PnPPageWebPart -Page $page -Section 1 -Column 2 -Order 3 -DefaultWebPartType “People” -WebPartProperties "{`"title`":`"Project Contacts`",`"persons`":[{`"id`":`"$projectManager`"}]}"

  Set-PnPPage -Identity "Home" -LayoutType $PageLayout -Publish

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.