Create Page Programmatically CSOM PowerShell script SharePoint 2013

Office 365 SharePoint 2013 Scripting CSOM. In this post, I will be sharing code to create a page using custom / out of the box page layout using CSOM and PowerShell script. If you have a requirement to create pages and looking for automated process here you go. Find the code below and I hope this will be useful.

Kindly add Page tag entry and specify page name and page layout input as well.

PS Code-

cls

$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$xmlFilePath = $(“$dp0\Inputs.xml”)
$xmldata = [xml](Get-Content($xmlFilePath));

$username = “samtest@microsoft.com”
$password = Read-Host -Prompt “Please enter your password” -AsSecureString

$url = $xmldata.WebSite.Url

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials

Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Publishing.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Taxonomy.dll”



if (!$clientContext.ServerObjectIsNull.Value)
{
Write-Host “Connected to SharePoint Online site: ‘$Url'” -ForegroundColor Green

$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()

}

function createPages([Microsoft.SharePoint.Client.Web] $web)
{
$rootWeb = $clientContext.Site.RootWeb
$clientContext.Load($rootWeb)
$clientContext.ExecuteQuery()

foreach($page in $xmldata.WebSite.Pages.Page)
{

$mpList = $rootWeb.Lists.GetByTitle(‘Master Page Gallery’)
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml = ‘<View><Query><Where><Eq><FieldRef Name=”FileLeafRef” /> `
<Value Type=”Text”>’+$page.Layout+'</Value></Eq></Where></Query></View>’

$items = $mpList.GetItems($camlQuery)
$clientContext.Load($items)

$clientContext.ExecuteQuery()

$articleLayoutItem = $items[0]
$clientContext.Load($articleLayoutItem)

$clientContext.ExecuteQuery()

$pubWeb = [Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($clientContext, $web)
$clientContext.Load($pubWeb)
$pagesList = $web.Lists.GetByTitle(‘Pages’)
$title =$page.Name
$pubPageInfo = New-Object Microsoft.SharePoint.Client.Publishing.PublishingPageInformation
$pubPageInfo.Name = $title.Replace(” “, “-“) + “.aspx”
$pubPageInfo.PageLayoutListItem = $articleLayoutItem
$pubPage = $pubWeb.AddPublishingpage($pubPageInfo)
$pubPage.ListItem.File.CheckIn(“”, [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$pubPage.ListItem.File.Publish(“”)

$clientContext.Load($pubPage)
$clientContext.ExecuteQuery()
Write-Host “Page Ceated: ‘$title'” -ForegroundColor Green

$listItem = $pubPage.get_listItem()
$clientContext.Load($listItem)
$clientContext.ExecuteQuery()

$file = $listItem.File
$file.CheckOut()
$clientContext.Load($file)
$clientContext.ExecuteQuery()

$listItem.Set_Item(“Title”, $title)

$listItem.Update()
$listItem.File.CheckIn(“”, [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$listItem.File.Publish(“”)
$clientContext.Load($listItem)
$clientContext.ExecuteQuery()
}

}
createPages $web

XML Inputs-
<?xml version=”1.0″ encoding=”utf-8″?>
<WebSite Url=”https://microsft.sharepoint.com/sites/test/” >
<Pages>
<Page Name=”HomePage” Layout=”CustomLayout.aspx” />
<Page Name=”Page1″ Layout=”BlankWebPartPage.aspx”/>
<Page Name=”Page2″ Layout=”BlankWebPartPage.aspx”/>
</Pages>
</WebSite>

Read some SharePoint tutorials:


Thanks,
Sambita

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →