Add WebPart On Modern Page PNP Powershell
On 07/05/2021
Add custom WebPart On Modern Page Powershell
List your webpart with :
$page = Get-PnPPage -Identity "aTestDisp2.aspx"
$page.AvailablePageComponents() select your webpart by name or guid
param([string]$siteUrl="https://myyTenant.sharepoint.com/sites/muSite", [string]$pageName="aTestDisp2", [string]$webPartName="Test - Display Item")
clear
Connect-PnPOnline -Url $siteUrl -UseWebLogin
#set your wenpart properties
$wpProps = @{webUrl="https://myyTenantsharepoint.com/sites/
muSite"
listId="b4d5f780-bd74-4cd5-b051-fed423e43125"
field="Title"
itemId="1"}
#get the page
$page = Get-PnPPage -Identity $pageName
# get the target webpart
$wp = $page.AvailablePageComponents() | Where-Object {$_.Name -eq $webPartName }
if($wp -eq $null)
{
Write-Host "webpart '$($webPartName)' not found " -ForegroundColor:Red
}
#Add webpart with default user, to column 1 in section 1
Add-PnPPageWebPart -Page $pageName -Component $wp -Section 1 -Column 1 -WebPartProperties $wpProps
Disconnect-PnPOnline
You can check webpart properties, in result