In this post I want to describe how to programmatically change Modern Page Banner Image The code below is written on JavaScript using JSOM (JavaScript Client Object Model) but it can be easily converted to C# (CSOM) and PowerShell as well. Let's say you've created some site using any provisioning mechanism (for example, PnP Provisioning Engine). And you also created multiple modern pages on the site and want to change the banner image for all of them. After some investigation I found a way to achieve that: list items in Site Pages library have a field named LayoutWebpartsContent. If you look at the value of this field you'll see something like:
So it's an HTML markup that contains interesting attribute data-sp-controldata. It looks like some escaped JSON. Let's unescape it and also replace : with :. The result looks like that:
If you proceed the same steps for the page that contains some banner image, you'll see a bit different result:
The differences here are in imageSource and imageSourceType properties (Actually you can also see different values for tranlateX and translateY if you changed focal point in the UI): imageSource contains server relative URL of the image that is used as a banner and imageSourceType contains 2 instead of 4. This information is enough to add (or replace) your own banner image on any page. First, you need to add the picture to some location on your tenant - SharePoint or OneDrive. Second, change imageSource and imageSourceType properties as shown below:
That's it. Now your page should have the banner you wanted. Have fun!
Comments