Rajesh
Rajesh 👨🏻‍💻developer, architect, consultant focussed on modernization, cognitive services, and conversational ai.

Bulk creation of Publishing Pages using PowerShell CSOM

Bulk creation of Publishing Pages using PowerShell CSOM"

In this post, we will look how to create publishing pages in bulk using the PowerShell CSOM. A CSV file will be used as an input which holds the meta data details of the page to be created. I have created a simple CSV file with the below columns matching my page layouts fields. For simplicity and time saving I just created two rows which means just two pages. (You said bulk :-) ) I’m using plain text for the Publishing Page Content column, I think it should be fine with the html code too.

SEE ALSO - Replacing YouTube, Vimeo, Vine Urls with embed code in pages.

Bulk creation of Publishing Pages using PowerShell CSOM

I’ll be using the below PowerShell CSOM code for this activity, which will read the rows in the CSV and create the corresponding publishing page. First loading the appropriate page layout from the catalogs folder.

1
2
3
4
5
6
$mpList = $rootWeb.Lists.GetByTitle('Master Page Gallery')
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml = '<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FileLeafRef" /><Value Type="Text">YOURLAYOUT.aspx</Value></Eq></Where></Query></View>'
$items = $mpList.GetItems($camlQuery)
$ctx.Load($items)
$ctx.ExecuteQuery()

Creating the publishing page

1
2
3
4
5
6
7
8
9
10
11
12
13
$pubPageInfo = New-Object Microsoft.SharePoint.Client.Publishing.PublishingPageInformation
$pubPageInfo.Name = $CSVitem.Name.Replace(" ", "-") + ".aspx"
$pubPageInfo.PageLayoutListItem = $tpLayoutItem
$pubPage = $pubWeb.AddPublishingpage($pubPageInfo)
$ctx.Load($pubPage)
$ctx.ExecuteQuery()
$listItem = $pubPage.get_listItem()
$ctx.Load($listItem)
$ctx.ExecuteQuery()
$listItem.Set_Item("Title", $CSVitem.Title)
$listItem.Set_Item("PublishingPageContent", $CSVitem.Description)
$listItem.Set_Item("SeoMetaDescription", $CSVitem.ContentSummary)
$listItem.Set_Item("SOME-MANAGED-METADATA-COLUMN",'GUID GOES HERE OR READ FROM CSV')

DOWNLOAD - Complete PowerShell file from here

The code is self explanatory so I’m not going line by line. Make sure to update the site url, page layout name inside the CAML query and other appropriate values. You can even have the page layout information inside the CSV file and use it for the page creation, in that case make sure to bring the page layout loading piece inside the for loop.

comments powered by Disqus