Powershell Script to set the banner in SharePoint Community site Modern pages RRS feed

  • Question

  • i'm not able to set banner in SharePoint Modern pages. Please share if any solution available.

    Tried below code SharePoint Community site  but no luck:

        Connect-PnPOnline https://xxxx/sites/xxxx2
    $item = (Get-PnPListItem -List "SitePages" -Id 1).FieldValues
        if($item["LayoutWebpartsContent"] -match 'data-sp-controldata="([^"]+)"'){
            # get substring w/ regex
            $temp = $item["LayoutWebpartsContent"] | Select-String -Pattern 'data-sp-controldata="([^"]+)"'
            $content = $temp.Matches.Groups[1].Value
            # replace [] bc sometimes it throws later
            $content = $content.replace("[","[").replace("]","]")
            # decode 
            $dec = [System.Web.HttpUtility]::HtmlDecode($content)
            $dec = $dec.replace("[","[").replace("]","]")
            # to JSON
            $jnContent = ConvertFrom-Json $dec
            #set values
            if (!$jnContent.serverProcessedContent) {
                $jnContent.serverProcessedContent = {};
            if (!$jnContent.serverProcessedContent.imageSources) {
                $jnContent.serverProcessedContent.imageSources = New-Object PSObject;
                $jnContent.serverProcessedContent.imageSources | add-member Noteproperty imageSource $newBannerUrl
                $jnContent.serverProcessedContent.imageSources | add-member Noteproperty imageSource $newBannerUrl
            $jnContent.serverProcessedContent.imageSources.imageSource = $newBannerUrl
            if (!$jnContent.properties) {
                        $jnContent.properties = {};
            $jnContent.properties.imageSourceType = 2;
            # from JSON
            $newContent = $jnContent | ConvertTo-Json -Compress
            $enc = [System.Web.HttpUtility]::HtmlEncode($newContent)
            $enc = $enc.replace("{","{").replace(":",":").replace("}","}").replace("[","[").replace("]","]")
            # replace full item property
            $fullContent = $item["LayoutWebpartsContent"].replace("[","[").replace("]","]");
            $fullContent = $fullContent -replace $content, $enc
            # set & update
            $item["LayoutWebpartsContent"] = $fullContent
            # not really sure if needed, but also update bannerURL
            Set-PnPListItem -List $listName -Id $itemId -Values @{"BannerImageUrl" = $newBannerUrl;}

    Thanks in Advance.

    Tuesday, October 23, 2018 3:32 PM


All replies

  • Hi manjioth,

    Please refer the demo below to set banner in Modern Page:

    Script to set the banner in SharePoint Modern pages


    Best Regards

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, October 24, 2018 8:49 AM
  • Thank You Jerry.

    But it's working for custom pages(Manually created) not for Home/default pages. Please suggest.

    Wednesday, October 24, 2018 12:10 PM