Question

We have an automated process of generating sample Modern pages for DEV/QA purposes. One challenge is to setup the page header's BannerImageUrl property to the Modern Page.

The obvious first attempt was to just specify BannerImageUrl field value with a URL to the image:

Set-PnPListItem -Identity 411 -List "SitePages" -Values @{"BannerImageUrl" ='https://...image.jpg'}

But even though BannerImageUrl field is successfully populated with a value, it does not actually do anything. When we navigate to the page - the header banner does not show up.

Then I have learned that we also need to modify the LayoutWebpartsContent field value in order to make it work. This is a sample code that I was trying. And it kind of works:

# SET IMAGE BANNER UR:

$fileUrl = 'https://xxx.sharepoint.com/Shared%20Documents/indenvironmentalmodule.jpg'
$InvorysURL = "https://xxx.sharepoint.com"
$tenantName = $([Uri]$InvorysURL).Authority.Replace(".sharepoint.com","")

$guidSite = (Get-PnPSite -Includes ID).ID.ToString();
$guidWeb = (Get-PnPWeb).ID.ToString();
$guidList = (Get-PnPList -Identity "Shared Documents").ID.ToString();
$pageTitle = "Test3"

$file = Get-PnPFile '/Shared Documents/indenvironmentalmodule.jpg' -AsListItem
$bannerGUID = $file["GUID"]
$bannerFileRef = $file["FileRef"]

$BannerImageUrlValue =  
$("https://{0}.sharepoint.com/_layouts/15/getpreview.ashx?guidSite={1}&guidWeb={2}&guidFile={3}, https://{0}.sharepoint.com/_layouts/15/getpreview.ashx?guidSite={1}&guidWeb={2}&guidFile={3}" -f $tenantName, $guidSite, $guidWeb, $bannerGUID);

$LayoutWebpartsContentValue = $("<div><div data-sp-controldata=""%7B%22id%22&#58;%22{0}%22,%22instanceId%22&#58;%22{0}%22,%22title%22&#58;%22Title%20Region%22,
%22description%22&#58;%22Title%20Region%20Description%22,%22serverProcessedContent%22&#58;
%7B%22htmlStrings%22&#58;%7B%7D,%22searchablePlainTexts%22&#58;%7B%7D,%22imageSources%22&#58;
%7B%22imageSource%22&#58;%22{1}%22%7D,%22links%22&#58;%7B%7D%7D,%22dataVersion%22&#58;%221.2%22,
%22properties%22&#58;%7B%22title%22&#58;%22{2}%22,%22imageSourceType%22&#58;2,%22siteId%22&#58;%22{3}%22,
%22webId%22&#58;%22{4}%22,%22listId%22&#58;%22%7B{5}%7D%22,%22uniqueId%22&#58;%22{6}%22,
%22translateX%22&#58;50,%22translateY%22&#58;50%7D%7D"" data-sp-canvascontrol="""">
</div></div>" -f "cbe7b0a9-3504-44dd-a3a3-0e5cacd07788", $bannerFileRef , $pageTitle, $guidSite, $guidWeb, $guidList, $bannerGUID);

$result = Set-PnPListItem -Identity 411 -List "SitePages" -Values @{"BannerImageUrl" =$BannerImageUrlValue; "LayoutWebpartsContent"= $LayoutWebpartsContentValue}

This seemed to work and I can see the banner in the page header displayed. But now I have other issues:

  • I cannot publish the page using PowerShell PnP
Set-PnPClientSidePage -Identity $pageTitle -Publish

Gives me an error:

Set-PnPClientSidePage : Unexpected character encountered while parsing value: %. Path '', line 0, position 0. At line:1 char:1 + Set-PnPClientSidePage -Identity $pageTitle -Publish + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Set-PnPClientSidePage], JsonReaderException + FullyQualifiedErrorId : >EXCEPTION,SharePointPnP.PowerShell.Commands.ClientSidePages.SetClientSidePage

  • Another concern is that LayoutWebpartsContentValue value seems to have some hardcoded attributes. For example dataVersion.

So my question is: Is there a clean way of setting up Page banner using PowerShell PnP that works and does not mess the modern page up?

Sample:

enter image description here

Was it helpful?

Solution

There is an easier way to do it :)

You can use the Set-PnPClientSidePage command as below, you can skip the translateX and translateY params:

Set-PnPClientSidePage -Identity "MyPage" -HeaderType Custom 
-ServerRelativeImageUrl "/sites/demo1/assets/myimage.png" -TranslateX 10.5 -TranslateY 11.0 -Publish

Reference - Set-PnPClientSidePage

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top