Create Page Programmatically CSOM PowerShell script SharePoint 2013

InfoPath alternatives for form designing SharePoint

Office 365 SharePoint 2013 Scripting CSOM. In this post I will be sharing code to create page using custom / out of the box page layout using CSOM and PowerShell script.

If you have requirement to create pages and looking for automate process here you go. Find the code below and I hope this will be useful.

Also check out:
– Office 365 Quick Development Design Tips and Tricks

– Office 365 Easy Form Development FORM7

– Timeline web part in SharePoint 2013

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>
Thanks,
Sambita

Similar SharePoint 2013 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 →

Leave a Reply