SOLVED

How to create Repost Page (Modern Page Library) with PowerShell

Steel Contributor

Dear all,

I tried to create a PowerShell script to create automatically "Modern Repost Page" pointing to the news page published into the SharePoint Publishing site (Global Intranet Site).

 

The manual creation from the "Modern News" webpart or "News" list page (https://[YourTenant].sharepoint.com/_layouts/15/news.aspx), I can also modify some metadata via the PowerShell code.

 

But I can't create correctly the Report Page via PowerShell, it's loaded with the current content type and the metadata are OK, but the News Webpart is not showing the automatically created Report Pages.

 

 

The current part of the code I created is inspired from what I found:

// pagesLibrary is List object for the "site pages" library of the site
ListItem item = pagesLibrary.RootFolder.Files.AddTemplateFile(serverRelativePageName, TemplateFileType.ClientSidePage).ListItemAllFields;

// Make this page a "modern" page
item["ContentTypeId"] = "0x0101009D1CB255DA76424F860D91F20E6C4118";
item["Title"] = System.IO.Path.GetFileNameWithoutExtension("mypage.aspx");
item["ClientSideApplicationId"] = "b6917cb1-93a0-4b97-a84d-7cf49975d4ec";
item["PageLayoutType"] = "Article";
item["PromotedState"] = "0";
item["CanvasContent1"] = "<div></div>";
item["BannerImageUrl"] = "/_layouts/15/images/sitepagethumbnail.png";
item.Update();
clientContext.Load(item);
clientContext.ExecuteQuery();

My adapted version is:

 

 

			[string]$NewsPageFileName = "/SitePages/"+ $MyCSVPublishingDate.ToString("yyyyMMdd") +'-'+ $CSVItem.NewsURL.Substring($CSVItem.NewsURL.LastIndexOf("/") + 1)
			Write-Host "                >> $($MyPagelistItems.Count) PageList Item Found, Need to be created [ $NewsPageFileName ]" -ForegroundColor Red # TO CREATE !!!

			$NewPageitem = $MyPagelist.RootFolder.Files.AddTemplateFile($NewsPageFileName, [Microsoft.SharePoint.Client.TemplateFileType]::ClientSidePage).ListItemAllFields

			# Make this page a "modern" page
			$NewPageitem["ContentTypeId"] = "0x0101009D1CB255DA76424F860D91F20E6C4118002A50BFCFB7614729B56886FADA02339B00FB61AB42CC88E741A501DF164E1EDB74";
			$NewPageitem["Title"] = $CSVItem.NewsTitle
			$NewPageitem["ClientSideApplicationId"] = "b6917cb1-93a0-4b97-a84d-7cf49975d4ec"
			$NewPageitem["_OriginalSourceUrl"] =  $MyCSVNewsURL
			$NewPageitem["Editor"] = $MyEditoruserAccount.Id
			$NewPageitem["Author"] = $MyEditoruserAccount.Id
			$NewPageitem["Description"] = $MyCSVNewsDescription
			$NewPageitem["BannerImageUrl"] = $MyCSVNewsPictureURL;
			$NewPageitem["Modified"] = $MyCSVPublishingDate;
			$NewPageitem.Update();
			$MyctxTemp.Load($NewPageitem);
			$MyctxTemp.ExecuteQuery();

The result from the file list view:RepostPageRepostPage

 

 

As you can see the bannerimage is loaded correctly, title also and description too, but when I click on the item to check and modify a value, I load the basic Empty SitePage and not the Repost Page.

 

Do you have any idea what I have to do to fix that ?

 

Thanks by advance.

 

Fabrice Romelard

10 Replies

Hey Fabrice,

 

I would recommend using PnP PowerShell as it has built-in methods to create and manipulate modern pages. The approach you've outlined assumes you've constructed the correct HTML for the canvascontent1 field...which is quite hard to do...The PnP team has built an API to manipulate modern pages and that API is also available via a series of PnP Powershell cmdlets.

 

Checkout https://hangconsult.com/2017/11/05/creating-a-new-client-side-page-with-pnp-powershell/ to get a view on how PnP Powershell can help you. When it comes to creating the web part properties JSON the easiest approach is to configure your web part using the WorkBench (_layouts/workbench.aspx), clicking on "***Web part data" and then copying the contents from the webPartData node.

Thanks a lot Bert for your message.

I looked the option to use the PnP to create that Repost Page, but the command available into PnP is not supporting the Repost Content Type

 


$page = Add-PnPClientSidePage -Name $pagename -LayoutType Home

 

The layouts are talking only the model to use into the modern page (Site Page), but the Repost is a dedicated Content Type.

This is why I started with the basic CSOM command instead.

 

Fab

Adding content type support when provisioning client side pages is something we do have in the PnP backlog...but in the meanwhile did you try the following:

- create page using default content type

- switch content type using set-pnplistitem (see https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/set-pnplistitem?view=sharepoint-ps)

Interesting idea, I tried to create the item first and change the content type list you are proposing, but it was not working because this Page library is a "document library" and the new-item (in CSOM mode) refuse to create it without a file.

 

But I will try to apply this idea using the PnP this time.

 

Thanks for your help.

best response confirmed by Fabrice Romelard (Steel Contributor)
Solution

Dear all,

 

I finally found how to do the Repost Page Creation with CSOM only.

The missing parts were the page layout to load correctly the page in edit mode, the PromotedState to be sure it will be considered as a news and the last ID to know exactly the target:

  • ["_OriginalSourceSiteId"]
  • ["_OriginalSourceWebId"]
  • ["_OriginalSourceListId"]
  • ["_OriginalSourceItemId"]

 

The code has to be similar to the following one:

 

			$NewPageitem["ContentTypeId"] = "0x0101009D1CB255DA76424F860D91F20E6C4118002A50BFCFB7614729B56886FADA02339B00874A802FBA36B64BAB7A47514EAAB232";
			$NewPageitem["PageLayoutType"] = "RepostPage"
			$NewPageitem["PromotedState"] = "2"
			$NewPageitem["Title"] = $CSVItem.NewsTitle
			$NewPageitem["ClientSideApplicationId"] = "b6917cb1-93a0-4b97-a84d-7cf49975d4ec"

			$NewPageitem["_OriginalSourceSiteId"] = $MyDestinationPageSiteColl.ID
			$NewPageitem["_OriginalSourceWebId"] = $MyDestinationPageweb.ID
			$NewPageitem["_OriginalSourceListId"] = $MyDestinationPagelist.ID
			$NewPageitem["_OriginalSourceItemId"] = $MyDestinationPageFileitem["UniqueId"].ToString()

			$NewPageitem["_OriginalSourceUrl"] =  $MyCSVNewsURL
			$NewPageitem["Editor"] = $MyEditoruserAccount.Id
			$NewPageitem["Author"] = $MyEditoruserAccount.Id
			$NewPageitem["Description"] = $MyCSVNewsDescription
			$NewPageitem["BannerImageUrl"] = $MyCSVNewsPictureURL;
			$NewPageitem["Modified"] = $MyCSVPublishingDate;
			$NewPageitem["Created"] = $MyCSVPublishingDate;
			$NewPageitem["Created_x0020_By"] = $MyEditoruserAccount.LoginName
			$NewPageitem["Modified_x0020_By"] = $MyEditoruserAccount.LoginName
			$NewPageitem["FirstPublishedDate"] = $MyCSVPublishingDate;
			$NewPageitem.Update();
			$MyctxTemp.Load($NewPageitem);
			$MyctxTemp.ExecuteQuery();

It's now working perfectly and the only point i'm fighting with is the picture display.

 

Thanks a lot for your help

 

Fab

 

 

Nice example!

We're using a custom content type that inherits from Repost Page and one additional detail that helped us was to set LayoutWebpartsContent to null. The seemingly magical fields (Link, Preview image, Title) and Edit button didn't appear until we cleared the content.

listItem["LayoutWebpartsContent"] = null; // C# CSOM

how i can use this in adding iframe in modern page 

@Abo-gabal 

 

iFrame is an object (webPart) available into modern page, and edit that kind of page is only available via PnP.

Sorry for that but I can't help you more.

 

Fab

There is a API to create repost pages:
_api/sitepages/pages/reposts

You can use it like this:
Invoke-PnPSPRestMethod -Url https://Contoso.sharepoint.com/sites/SiteName/_api/sitepages/pages/reposts -Method Post -Content @{ Title = "Repost Title"; OriginalSourceUrl = "https://www.RedirectUrl.com"; Description = "Description Text" }

The accepted parameters are:
BannerImageUrl: "https://some.absolute/path/to/an/image.jpg",
IsBannerImageUrlExternal: true,
Description: "My Description",
Title: "This is my title!",
OriginalSourceUrl: "https://absolute/path/to/article"
OriginalSourceItemId : 00000000-0000-0000-0000-000000000000
OriginalSourceListId : 00000000-0000-0000-0000-000000000000
OriginalSourceSiteId : 00000000-0000-0000-0000-000000000000
OriginalSourceWebId : 00000000-0000-0000-0000-000000000000

Making a Get to this API it will return the existing news.
I'm not sure but I don't think adding it using the suggested method on this post makes it appear the replys of this API.
1 best response

Accepted Solutions
best response confirmed by Fabrice Romelard (Steel Contributor)
Solution

Dear all,

 

I finally found how to do the Repost Page Creation with CSOM only.

The missing parts were the page layout to load correctly the page in edit mode, the PromotedState to be sure it will be considered as a news and the last ID to know exactly the target:

  • ["_OriginalSourceSiteId"]
  • ["_OriginalSourceWebId"]
  • ["_OriginalSourceListId"]
  • ["_OriginalSourceItemId"]

 

The code has to be similar to the following one:

 

			$NewPageitem["ContentTypeId"] = "0x0101009D1CB255DA76424F860D91F20E6C4118002A50BFCFB7614729B56886FADA02339B00874A802FBA36B64BAB7A47514EAAB232";
			$NewPageitem["PageLayoutType"] = "RepostPage"
			$NewPageitem["PromotedState"] = "2"
			$NewPageitem["Title"] = $CSVItem.NewsTitle
			$NewPageitem["ClientSideApplicationId"] = "b6917cb1-93a0-4b97-a84d-7cf49975d4ec"

			$NewPageitem["_OriginalSourceSiteId"] = $MyDestinationPageSiteColl.ID
			$NewPageitem["_OriginalSourceWebId"] = $MyDestinationPageweb.ID
			$NewPageitem["_OriginalSourceListId"] = $MyDestinationPagelist.ID
			$NewPageitem["_OriginalSourceItemId"] = $MyDestinationPageFileitem["UniqueId"].ToString()

			$NewPageitem["_OriginalSourceUrl"] =  $MyCSVNewsURL
			$NewPageitem["Editor"] = $MyEditoruserAccount.Id
			$NewPageitem["Author"] = $MyEditoruserAccount.Id
			$NewPageitem["Description"] = $MyCSVNewsDescription
			$NewPageitem["BannerImageUrl"] = $MyCSVNewsPictureURL;
			$NewPageitem["Modified"] = $MyCSVPublishingDate;
			$NewPageitem["Created"] = $MyCSVPublishingDate;
			$NewPageitem["Created_x0020_By"] = $MyEditoruserAccount.LoginName
			$NewPageitem["Modified_x0020_By"] = $MyEditoruserAccount.LoginName
			$NewPageitem["FirstPublishedDate"] = $MyCSVPublishingDate;
			$NewPageitem.Update();
			$MyctxTemp.Load($NewPageitem);
			$MyctxTemp.ExecuteQuery();

It's now working perfectly and the only point i'm fighting with is the picture display.

 

Thanks a lot for your help

 

Fab

 

 

View solution in original post