Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

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

$
0
0

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:

Param
(
  [Parameter(Mandatory=$true)]
    [string]
    $listName,
    [Parameter(Mandatory=$true)]
    [int] 
    $itemId,
    [Parameter(Mandatory=$true)]
    [string]
    $newBannerUrl
    )
Begin
{
    Connect-PnPOnline https://xxxx/sites/xxxx2
    
}
Process
{
$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
        }
        if(!$jnContent.serverProcessedContent.imageSources.imageSource){
            $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
        $fullContent.replace("[","[").replace("]","]")
         
        # set & update
        $item["LayoutWebpartsContent"] = $fullContent
        $item.Update()
         
        # not really sure if needed, but also update bannerURL
        Set-PnPListItem -List $listName -Id $itemId -Values @{"BannerImageUrl" = $newBannerUrl;}
    }
    }
 

Thanks in Advance.



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>