Last active
September 5, 2019 23:12
-
-
Save fluxdigital/258dc305d7a6323ae000036d37f2e90c to your computer and use it in GitHub Desktop.
Copies Final Layout Back To Page Template and optionally resets the page final renderings back to the page templates standard values
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 Copy-Page-FinalLayout-To-Template([string]$pagePath,[switch]$ResetLayout){ | |
#get the page item | |
$item = Get-Item -Path "master:$pagePath" | |
#get the final layout from the page | |
$finalLayoutField = New-Object -TypeName "Sitecore.Data.Fields.LayoutField" -ArgumentList $Item.Fields[[Sitecore.FieldIDs]::FinalLayoutField] | |
if ($finalLayoutField -eq $null) { | |
Write-Error "Couldn't find final layout on: $($Item.Name)" | |
} | |
if ($finalLayoutField.Value -eq $null -or $finalLayoutField.Value -eq "") { | |
return; | |
} | |
#get the items template and standard values on the template | |
$template = Get-ItemTemplate -ID $item.ID | |
$templateItem = Get-Item -Path "master:$($template.ID)" | |
$templateStandardValuesPath = "$($templateItem.Paths.FullPath)/__Standard values"; | |
$standardValuesItem = Get-Item -Path "$($templateItem.Paths.FullPath)/__Standard values" | |
#copy the final layout details back to the the renderings field on the standard values | |
$finalLayoutDefinition = [Sitecore.Layouts.LayoutDefinition]::Parse($finalLayoutField.Value) | |
$standardValuesItem.Editing.BeginEdit() | |
$standardValuesItem["__Renderings"] = $finalLayoutDefinition.ToXml(); | |
$standardValuesItem.Editing.EndEdit() | |
Write-Host "Template Standard Values Updated: $($templateItem.Name)" | |
#reset the page final renderings back to the page templates standard values | |
if($ResetLayout){ | |
Reset-ItemField -Item $Item -Name "__Final Renderings" -IncludeStandardFields | |
Write-Host "Page Final Renderings Reset To Standard Values: $($item.Name)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment