PNP powershell SharePoint Online设置现代页面bannerimageurl

时间:2017-09-25 04:13:38

标签: powershell sharepoint sharepoint-online

我的目标是使用PNP命令行开关为页面设置SharePoint在线现代bannerimageurl属性。

首先,我得到一个页面列表及其当前标题和bannerimageurl值

# Get alist of all pages and their banner URLs
$items = Get-PnPListItem -List "SitePages" -Fields ID,Title,BannerImageUrl
$items | %{new-object PSObject -Property @{Id=$_["ID"];Title=$_["Title"];BannerImageUrl=$_["BannerImageUrl"].Url}} | select ID,Title,BannerImageUrl

但即使我然后运行以下代码来设置一页的BannerImage(比如ID2)

Set-PnPListItem -List "SitePages" -Id 2 -Values @{"BannerImageUrl" = " https://contoso.sharepoint.com/sites/mycomsite3/bannerimages/bread-braid-tedster-sml.jpg";}

当我再次运行以下内容时,项目2显示为具有已更改的BannerImageUrl

$items = Get-PnPListItem -List "SitePages" -Fields ID,Title,BannerImageUrl
$items | %{new-object PSObject -Property @{Id=$_["ID"];Title=$_["Title"];BannerImageUrl=$_["BannerImageUrl"].Url}} | select ID,Title,BannerImageUrl

但是当我实际在浏览器中查看第2项的页面时,横幅图像没有变化吗?

当我设置BannerImageUrl时,请告诉我我做错了什么。

您的经验和知识被广泛接受。

1 个答案:

答案 0 :(得分:1)

我已根据JS解决方案here为完全相同的问题编写了PS函数,以防您仍然需要它:

function UpdateBannerImage {
param(
    [string]$listName,
    [int]$itemId,
    [string]$newBannerUrl
)
#get list item
$item = Get-PnPListItem -List $listName -Id $itemId -Fields LayoutWebpartsContent, BannerImageUrl
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)
    # from 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

    # need to always create new properties, otherwise nothing changes
    $curTitle = "";
    if($jnContent.properties){
        $curTitle = $jnContent.properties.title;
    }
        $jnContent.properties = New-Object PSObject;
    $jnContent.properties | add-member Noteproperty title $curTitle
    $jnContent.properties | add-member Noteproperty imageSourceType 2

    # to 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; }
    }
}

这里有新的抱歉,如果我弄乱了格式,也上传了安全here:P