pnp / PnP-Sites-Core

Microsoft 365 Dev PnP Core component (.NET) targeted for increasing developer productivity with CSOM based solutions.
Other
415 stars 644 forks source link

SharePoint Online modern page bannerimageurl set but not set issue #1464

Open erwinvanhunen opened 6 years ago

erwinvanhunen commented 6 years ago

From @DorjeM on September 25, 2017 21:13

I think I’ve discovered a bug/issue with setting the BannerImageUrl for SharePoint online Modern Pages.

I expect that when I run the following the banner for page ID2 will change

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

When I check using the following code the page ID2 reports it’s banner has been changed.

$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 the banner of page ID2 has not changed.

My goal is to use PNP cmdlets to set the banner image on new pages, to an image I’ve already uploaded to a library within the site.

Cmdlet versions

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     2.18.17... SharePointPnPPowerShell2016         {Add-PnPContentType, Add-PnPContentTypeToDocumentSet,
    Directory: C:\Users\<user>\AppData\Local\Apps\SharePointPnPPowerShellOnline\Modules
ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     2.18.17... SharePointPnPPowerShellOnline       {Add-PnPClientSidePage, Add-PnPClientSidePageSection,
    Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     2.18.17... SharePointPnPPowerShellOnline       {Add-PnPClientSidePage, Add-PnPClientSidePageSection,
Binary     2.17.17... SharePointPnPPowerShellOnline       {Add-PnPContentType, Add-PnPContentTypeToDocumentSet,

Cmdlet install

Please tell me what I'm doing wrong when I set the BannerImageUrl. Your experience and knowledge greatly accepted.

Copied from original issue: SharePoint/PnP-PowerShell#1106

erwinvanhunen commented 6 years ago

From @DorjeM on September 28, 2017 22:4

I've checked using SharePoint designer and when I run code like the following

Set-PnPListItem -List "SitePages" -Id 29 -Values @{"BannerImageUrl" = "";}

The code within the ASPX page is modified to

<mso:BannerImageUrl msdt:dt="string"></mso:BannerImageUrl>

But the page when viewed in the borwser doesn't show no bannerimage.

Same if I copy the <mso:BannerImageUrl value from another page and use set-PnPListItem to insert it into a page ID29 . The ASPX page changes but the page doesn't display the new banner image. e.g.

Set-PnPListItem -List "SitePages" -Id 29 -Values @{"BannerImageUrl" = "https://brighterdays.sharepoint.com/_layouts/15/getpreview.ashx?guidSite=7b7f24b2-a8f8-47b9-ba32-99d7d24ea2bf&guidWeb=a6b51390-845d-4345-95c7-15b16d9048f9&guidFile=0b5854ac-fc5e-41d4-a772-a9fa0eae2b80, https://brighterdays.sharepoint.com/_layouts/15/getpreview.ashx?guidSite=7b7f24b2-a8f8-47b9-ba32-99d7d24ea2bf&guidWeb=a6b51390-845d-4345-95c7-15b16d9048f9&guidFile=0b5854ac-fc5e-41d4-a772-a9fa0eae2b80";}

Hope this helps

erwinvanhunen commented 6 years ago

From @chrisobriensp on October 4, 2017 10:42

I'm also having issues trying to set the BannerImageUrl. I've tried various things, including:

It seems that the LayoutWebpartsContent property is also relevant here (i.e. things will work if some modification to this is performed). Through some reverse-engineering of this property (including a lot of XML/JSON parsing, I've been able to get things working under certain conditions) - however, we need a reliable, simple abstraction onto this.

PnP team - can you help at all? Even if you can tell us what is needed before your method arrives, that would be great :) @jansenbe @VesaJuvonen

Rafaelki commented 6 years ago

LayoutWebpartsContent contains a key called imageSourceType that should be 2 for custom images, 4 for default image, 0 for no image.

sukantasaha06 commented 6 years ago

Thanks Rafaelki . can't find the property LayoutWebpartsContent . How to set this property of a modrn page using C#.

MalteJohansson commented 6 years ago

Just wondering if we have an update on this issue? I've tried to change the BannerImageUrl both by cmdlets and xml-provisioning but currently I'm stuck.

jansenbe commented 6 years ago

So you shouldn't manually set banner image url: when you save a page we'll apply similar logic as the out-of-the-box UI, meaning we'll try to find an image and text on the page and use that to configure the bannerimageurl and description properties of the page. Technically speaking you could override the bannerimageurl property after the page has been saved, but would not recommend doing that as a page update might override that again.

alexchx commented 6 years ago

@Rafaelki @jansenbe We are blocked by this issue too, our requirements is to create the news article and set the banner image programmatically, but the LayoutWebpartsContent is always null, could you advise how to proceed or work around?

Rafaelki commented 6 years ago

Hi @alexchx , this code is working for me:

$listName = 'sitepages'
$listItemId = 6

$getPageFields = (get-PnPListItem -List $listName -Id $listItemId).FieldValues
$lwc = $getPageFields.LayoutWebpartsContent
$newLwc = $lwc -replace "&quot;layoutType&quot;&#58;&quot;FullWidthImage&quot;", "&quot;layoutType&quot;&#58;&quot;NoImage&quot;"
$newLwc = $newLwc -replace "imageSource&quot;&#58;&quot;(.*?)&quot;", "imageSource&quot;&#58;&quot;&quot;"
Set-PnPListItem -List $listName -Identity $listItemId -Values @{"LayoutWebpartsContent" = $newLwc}
alexchx commented 6 years ago

@Rafaelki Thank you and sorry for the late response, I just noticed that we're using old version of PnP, just upgraded it and all dependencies, now your workaround works well.

By the way, I noticed the banner could be set as below too, but it will cause another trouble that the webparts added by ClientSidePage.AddControl will be all ignored.

var page = new ClientSidePage(context) { PageTitle = title };
page.PageHeader.ImageServerRelativeUrl = banner;
czullu commented 5 years ago

@jansenbe The provisioning of FieldValues on ClientSidePage to File Propertybag does not work for Complex and Tokenized Values because of this Code Block from Line 533 of ObjectClientSidePages does not use Parser

                        foreach (var pageProperty in clientSidePage.Properties)
                        {
                            if (!string.IsNullOrEmpty(pageProperty.Key))
                            {
                                pageFile.Properties[pageProperty.Key] = pageProperty.Value;
                            }
                        }

image