Ashish Prajapati
Ashish Prajapati

Reputation: 93

Add webpart in Sharepoint Modern page using Powershell

How can I add webpart using Powershell script in SharePoint online modern page?

Add Webpart using PowerShell script, I tried many more times to do that but got errors and warnings. can you help me solve that issue?

Upvotes: 0

Views: 477

Answers (1)

jleture
jleture

Reputation: 1088

You need to use the command Add-PnPPageWebPart from the PowerShell module Pnp.PowerShell

Sample of code:

$siteUrl = "https://my-tenant.sharepoit.com/sites/my-site"
Connect-PnPOnline -Url $siteUrl -Interactive

$pageUrl = "home.aspx"
$page = Get-PnPPage -Identity $pageUrl

Add-PnPPageTextPart -Page $page -Text "Hello world!"

Add-PnPPageWebPart -Page $page -DefaultWebPartType "List" -WebPartProperties @{ webRelativeListUrl = "/SitePages"}

Console:

PowerShell script

Result:

SharePoint webpart page

What is your problem?

What kind of errors do you have?

Upvotes: 1

Related Questions