Skip to content

Instantly share code, notes, and snippets.

@rjesh-git
Last active August 29, 2015 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjesh-git/da5e78f1a8cb2a8459a0 to your computer and use it in GitHub Desktop.
Save rjesh-git/da5e78f1a8cb2a8459a0 to your computer and use it in GitHub Desktop.
Creating publishing pages using PowerShell CSOM
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"
$siteUrl = "https://YOUR SITE URL"
# - use appropriate login methods
#$username = Read-Host -Prompt "Enter Username"
#$password = Read-Host -Prompt "Enter password" -AsSecureString
#$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
# - Uncomment above lines incase of SharePoint online
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
#$ctx.Credentials = $credentials
$rootWeb = $ctx.Site.RootWeb
$ctx.Load($rootWeb)
$ctx.ExecuteQuery()
$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()
$tpLayoutItem = $items[0]
$ctx.Load($tpLayoutItem)
$web = $ctx.Web
$pubWeb = [Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($ctx, $web)
$ctx.Load($pubWeb)
$pagesList = $ctx.Web.Lists.GetByTitle('Pages')
$CSVitems = Import-Csv "YOUR-CSV-FILE.csv"
FOREACH ($CSVitem in $CSVitems) {
Write-Host "Creating.." $CSVitem.Name
$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')
$listItem.Update()
# - Uncomment the below based on your library version settings
#$listItem.File.CheckIn("Inital Checkin from import tool", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
#$listItem.File.Publish("Migrated from tool")
#$listItem.File.Approve("Auto approval by tool")
$ctx.Load($listItem)
$ctx.ExecuteQuery()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment