简体   繁体   English

PNP powershell SharePoint Online 设置新式页面 bannerimageurl

[英]PNP powershell SharePoint Online set modern page bannerimageurl

My goal is to use PNP commandlets to set the SharePoint online modern bannerimageurl property for a page.我的目标是使用 PNP commandlet 为页面设置 SharePoint Online 现代 bannerimageurl 属性。

First I get a list of the pages and their current title and bannerimageurl values首先,我获取页面列表及其当前标题和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

But even if I then run the following code to set the BannerImage of one page (say ID2)但即使我然后运行以下代码来设置一页的 BannerImage(比如 ID2)

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

When I run the following again the item 2 shows up as having a changed BannerImageUrl当我再次运行以下命令时,项目 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

BUT when I actually view the page in the browser that is item 2 there has been no change to the banner image ??但是当我在浏览器中实际查看页面时,项目 2 横幅图像没有变化??

Please tell me what I'm doing wrong when I set the BannerImageUrl.请告诉我设置 BannerImageUrl 时我做错了什么。

Your experience and knowledge greatly accepted.您的经验和知识得到了极大的认可。

I've written a PS Function for that exact same problem based on the JS Solution here , in case you still need it:我已经根据此处的 JS 解决方案为完全相同的问题编写了一个 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; }
    }
}

new here sorry if i mess up the format, also uploaded for safety here :P新在这里抱歉,如果我弄乱了格式,为了安全也在这里上传:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell PnP - 在线登录 Sharepoint 并下载文件 - Powershell PnP - login to Sharepoint online and download file SharePoint OnLine - PowerShell PNP - 列出具有属性的文件夹 - SharePoint OnLine - PowerShell PNP - List folders that have attribute 使用 pnp 更新 SharePoint 在线文档库列属性 PowerShell - update SharePoint online document library column property using pnp PowerShell Sharepoint 在线 - MoveFile - pnp.powershell 状态更新 - Sharepoint online - MoveFile - pnp.powershell Status Updates 使用PnP PowerShell for SharePoint Online在文档库上设置“说明” - Setting the “Description” on a Document Library using PnP PowerShell for SharePoint Online 需要使用 PnP 在线 SharePoint 中添加语言翻译器 PowerShell - Need to Add Translators for Language in SharePoint Online using PnP PowerShell 使用 Powershell PNP 获取 Sharepoint 在线文档库中特定文件夹中的项目 - Get items in specific folder in Sharepoint online document library with Powershell PNP Powershell PnP Sharepoint - Powershell PnP Sharepoint 您可以使用Powershell PnP设置SharePoint网站的区域设置和语言吗 - Can you set a SharePoint site Locale and Languages using Powershell PnP 使用 Powershell(选项 1 - 使用 PnP.Powershell)将文件上传到 Sharepoint Online (Microsoft 365) - Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 1-Using PnP.Powershell)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM