2

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

1

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

| improve this answer | |
  • Does it work for you? I have tried this: $res = Set-PnPClientSidePage -Identity "Test4" -HeaderType Custom -ServerRelativeImageUrl "/Shared%20Documents/indenvironmentalmodule.jpg" -TranslateX 10.5 -TranslateY 11.0 -Publish $item = Get-PnPListItem -Id $res.PageListItem.Id -List "SitePages" $item["BannerImageUrl"] . It just wipes out an existing banner and replaces is with a defailt generic image /_layouts/15/images/sitepagethumbnail.png. Is there some trick I need to know? maybe location of the image or something like that? – Denis Molodtsov Aug 30 '19 at 16:09
  • The image needs to reside in the same site collection , can you check again with a location such as /sites/test//Shared%20Documents/indenvironmentalmodule.jpg and check – Gautam Sheth Aug 30 '19 at 16:12
  • The page is located in the root site , in the /, in the 'SitePages' and the location of the banner is in the same site collection, under /Shared%20Documents/. Does it matter if it's Shared Documents or SiteAssets? – Denis Molodtsov Aug 30 '19 at 16:14
  • It works perfectly, if I put the banner image in the SiteAssets. Example: Set-PnPClientSidePage -Identity "Test4" -HeaderType Custom -ServerRelativeImageUrl "/SiteAssets/airport.jpg" -TranslateX 10.5 -TranslateY 11.0 -Publish – Denis Molodtsov Aug 30 '19 at 16:17
  • 1
    Ahh okk, glad to know it worked :) – Gautam Sheth Aug 30 '19 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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