Created
May 13, 2015 19:19
-
-
Save beckettkev/a4fe23b2efa1df02a825 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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