Skip to content

Instantly share code, notes, and snippets.

@beckettkev
Created May 13, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beckettkev/a4fe23b2efa1df02a825 to your computer and use it in GitHub Desktop.
Save beckettkev/a4fe23b2efa1df02a825 to your computer and use it in GitHub Desktop.
function Set-AvailableAndDefaultPageLayouts
{
param (
[Microsoft.SharePoint.Client.ClientContext]$clientContext,
[Microsoft.SharePoint.Client.Web]$web,
[array]$pageLayoutUrls,
[string]$defaultPageLayoutUrl
)
process
{
Write-Host "Setting available page layouts and default page layout on " $web.Url -NoNewLine
$allProps = $web.AllProperties
$serverRelativeSiteUrl = $web.ServerRelativeUrl
$clientContext.Load($web)
$clientContext.Load($allProps)
$clientContext.ExecuteQuery()
$defaultPageLayoutXml = ""
$pageLayoutXml = "<pagelayouts>"
$pageLayoutUrls | % {
$pageLayoutXml = $pageLayoutXml + "<layout"
$fileUrl = $_
$serverRelativeSiteUrl =$clientContext.Site.RootWeb.ServerRelativeUrl
$file =$clientContext.Site.RootWeb.GetFileByServerRelativeUrl($serverRelativeSiteUrl + $fileUrl)
$clientContext.Load($file)
$clientContext.ExecuteQuery()
$listItemFields = $file.ListItemAllFields
$clientContext.Load($listItemFields)
$clientContext.ExecuteQuery()
$fileUrl = $fileUrl.TrimStart("/")
$pageLayoutXml += " guid=`"" +$listItemFields.FieldValues["UniqueId"] + "`" url=`"" + $fileUrl +"`" />"
if ($_ -eq $defaultPageLayoutUrl) {
$defaultPageLayoutXml = "<layout guid=`"" +$listItemFields.FieldValues["UniqueId"] + "`" url=`"" + $fileUrl +"`" />"
}
}
$pageLayoutXml += "</pagelayouts>"
if ($pageLayoutXml -ne "<pagelayouts></pagelayouts>") {
$allProps["__PageLayouts"] = $pageLayoutXml
}
if ($defaultPageLayoutXml -ne "") {
$allProps["__DefaultPageLayout"] = $defaultPageLayoutXml
}
$web.Update()
$clientContext.ExecuteQuery()
Write-Host " Done" -ForegroundColor Green
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment