In this article, you will learn how we can create a publishing page using CSOM with PowerShell for any SharePoint platform site.
Steps Involved:
The following section explains you the flow for creating a publishing page.
Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.Publishing.dll.
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
Initialize client context object with the site URL.
$siteURL = ""
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context.
# Not required for on premise site - Start
$userId = ""
$pwd = Read-Host -Prompt "Enter password" -AsSecureString
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
$ctx.credentials = $creds
# Not required for on premise site - End
If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server.
Then check if the page already exists.
Get the page library using GetByTitle method from the list collection.
Then initialize the query object and set the query using view xml method.
Get the items using GetItems method.
Load and execute the query.
Then check the items count.
$pageName = "TestPage1.aspx"
$lists = $ctx.web.Lists
$list = $lists.GetByTitle("Pages")
$query = New-Object Microsoft.SharePoint.Client.CamlQuery $query.ViewXml = "<View><Query><Where><Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>"+$pageName+"</Value></Contains></Where></Query></View>"
$listItems = $list.GetItems($query)
$ctx.load($listItems)
$ctx.executeQuery()
if($listItems.Count -le 0)
{
Write-Host "Page doesn't exists! Further Code goes here"
}
If the page is not found, we can create it using the existing page layout.
By default page layouts are present in master pages gallery.
Get the page layout library and filter out page layout item using the query object.
Once the query is set, get the necessary page layout using GetItems method.
Since, it returns the item collection, we will take the first item from the collection.
$pageLayoutList = $ctx.web.Lists.GetByTitle("Master Page Gallery")
$pageLayoutQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$pageLayoutQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>BlankWebPartPage.aspx</Value></Eq></Where></Query></View>" $pageLayouts = $pageLayoutList.GetItems($pageLayoutQuery)
$ctx.Load($pageLayouts)
$ctx.ExecuteQuery()
$pageLayout = $pageLayouts[0]
Get the publishing web by loading the existing context and the web context.
# Get the publishing Web $pubWeb = [Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($ctx, $ctx.Web)
$ctx.Load($pubWeb)
For creating page,
Set the necessary page information using PublishingPageInformation object. The necessary parameters are page name and the page layout item.
Once parameters set, add the page object using AddPublishingPage method with publishing web.
Then load and execute the query.
$pageInfo = New-Object Microsoft.SharePoint.Client.Publishing.PublishingPageInformation
$pageInfo. Name = $pageName
$pageInfo.PageLayoutListItem = $pageLayout
$page = $pubWeb.AddPublishingPage($pageInfo)
$ctx.Load($page)
$ctx.ExecuteQuery()
The page will be created and present under the pages library. By default, all the publishing pages will be present in the pages library.
Comments