我的目标是使用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时我做错了什么。

您的经验和知识深深地被接受。

最佳答案

我已经基于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

08-17 13:46