Skip to content

Instantly share code, notes, and snippets.

@MartinMiles
Last active June 4, 2025 16:42
Show Gist options
  • Save MartinMiles/e807212fe9f1217752d9d94f37e68d62 to your computer and use it in GitHub Desktop.
Save MartinMiles/e807212fe9f1217752d9d94f37e68d62 to your computer and use it in GitHub Desktop.
# -----------------------------------------------
# Script: Get-FinalLayoutXml-ForPage.ps1
# Purpose: Retrieve the merged (“Final”) layout XML
# for a given page item—combining shared,
# versioned, and standard-values presentations.
# Target Item: /sitecore/content/Zont/Habitat/Home/AAA
# Prerequisites: Run inside Sitecore PowerShell ISE or
# using Sitecore PowerShell Extensions context.
# -----------------------------------------------
# 1. PARAMETERS (hard-coded for this example)
param(
# Change this path if you need a different item
[string]$PageItemPath = "/sitecore/content/Zont/Habitat/Home/AAA"
)
# 2. MOUNT MASTER PSDrive IF NECESSARY
if (-not (Get-PSDrive -Name master -ErrorAction SilentlyContinue)) {
New-PSDrive -Name master -PSProvider Sitecore -Root "/" -Database "master" -ErrorAction Stop | Out-Null
}
# 3. GET THE MASTER DATABASE AND ITEM
$database = [Sitecore.Configuration.Factory]::GetDatabase("master")
if (-not $database) {
Write-Error "❌ Could not load 'master' database. Aborting."
return
}
$pageItem = $database.GetItem($PageItemPath)
if (-not $pageItem) {
Write-Error "❌ Item not found at path: $PageItemPath`nPlease check the path and try again."
return
}
# 4. HELPER FUNCTION: Retrieve the “Final Layout” XML
function Get-FinalLayoutXml {
<#
.SYNOPSIS
Returns the merged layout XML (versioned, shared, or standard values).
.PARAMETER item
The Sitecore item for which to fetch the final layout definition.
.OUTPUTS
[string] — Raw layout XML (all renderings, devices, etc)
#>
param (
[Parameter(Mandatory = $true)]
[Sitecore.Data.Items.Item]$item
)
# 4.1 Check versioned (Final Renderings) on the item
$finalLayoutField = $item.Fields["__Final Renderings"]
if ($finalLayoutField -and -not [string]::IsNullOrWhiteSpace($finalLayoutField.Value)) {
return $finalLayoutField.Value
}
# 4.2 Fallback to shared (Renderings) on the item
$sharedLayoutField = $item.Fields["__Renderings"]
if ($sharedLayoutField -and -not [string]::IsNullOrWhiteSpace($sharedLayoutField.Value)) {
return $sharedLayoutField.Value
}
# 4.3 Neither versioned nor shared exist on item => check Standard Values
$template = $item.Template
if (-not $template) {
Write-Error "❌ Item has no template. Cannot retrieve standard values."
return $null
}
$stdValuesItem = $template.StandardValues
if (-not $stdValuesItem) {
Write-Error "❌ Could not find Standard Values for template: $($template.Name)"
return $null
}
# 4.3.1 Check versioned in Standard Values (rarely used; usually blank)
$stdFinalField = $stdValuesItem.Fields["__Final Renderings"]
if ($stdFinalField -and -not [string]::IsNullOrWhiteSpace($stdFinalField.Value)) {
return $stdFinalField.Value
}
# 4.3.2 Then check shared in Standard Values
$stdSharedField = $stdValuesItem.Fields["__Renderings"]
if ($stdSharedField -and -not [string]::IsNullOrWhiteSpace($stdSharedField.Value)) {
return $stdSharedField.Value
}
# 4.4 If we reach here, nothing was defined anywhere
Write-Warning "⚠️ No layout XML found on item or its standard values."
return $null
}
# 5. GET AND OUTPUT THE FINAL LAYOUT XML FOR THE PAGE ITEM
$layoutXml = Get-FinalLayoutXml -item $pageItem
if ($layoutXml) {
# 5.1 Print to console
Write-Host "🗸 Final Layout XML for item '$($pageItem.Paths.FullPath)':`n" -ForegroundColor Green
Write-Output $layoutXml
# 5.2 OPTIONALLY: Uncomment to save to a file instead
# $outFile = "C:\Temp\AAA_FinalLayout.xml"
# $layoutXml | Out-File -FilePath $outFile -Encoding UTF8
# Write-Host "✅ Layout XML written to: $outFile"
}
else {
Write-Error "❌ Failed to retrieve any layout XML for '$($pageItem.Paths.FullPath)'."
}
param(
[string]$itemPath = "/sitecore/content/Zont/Habitat/Home/AAA"
)
Set-Location -Path $PSScriptRoot
$config = Get-Content -Raw -Path ./config.LOCAL.json | ConvertFrom-Json
Write-Output "ConnectionUri: $($config.connectionUri)"
Write-Output "Username : $($config.username)"
Write-Output "SPE Remoting Secret : $($config.SPE_REMOTING_SECRET)"
Write-Output "Get item fields of '$itemPath'..."
Import-Module -Name SPE
$session = New-ScriptSession -ConnectionUri $config.connectionUri -Username $config.username -SharedSecret $config.SPE_REMOTING_SECRET
Invoke-RemoteScript -Session $session -ScriptBlock {
# 2. MOUNT MASTER PSDrive IF NECESSARY
if (-not (Get-PSDrive -Name master -ErrorAction SilentlyContinue)) {
New-PSDrive -Name master -PSProvider Sitecore -Root "/" -Database "master" -ErrorAction Stop | Out-Null
}
# 3. GET THE MASTER DATABASE AND ITEM
$database = [Sitecore.Configuration.Factory]::GetDatabase("master")
if (-not $database) {
Write-Error "? Could not load 'master' database. Aborting."
return
}
$pageItem = $database.GetItem($Using:itemPath)
if (-not $pageItem) {
Write-Error "? Item not found at path: $Using:itemPath`nPlease check the path and try again."
return
}
# 4. HELPER FUNCTION: Retrieve the �Final Layout� XML
function Get-FinalLayoutXml {
<#
.SYNOPSIS
Returns the merged layout XML (versioned, shared, or standard values).
.PARAMETER item
The Sitecore item for which to fetch the final layout definition.
.OUTPUTS
[string] � Raw layout XML (all renderings, devices, etc)
#>
param (
[Parameter(Mandatory = $true)]
[Sitecore.Data.Items.Item]$item
)
# 4.1 Check versioned (Final Renderings) on the item
$finalLayoutField = $item.Fields["__Final Renderings"]
if ($finalLayoutField -and -not [string]::IsNullOrWhiteSpace($finalLayoutField.Value)) {
return $finalLayoutField.Value
}
# 4.2 Fallback to shared (Renderings) on the item
$sharedLayoutField = $item.Fields["__Renderings"]
if ($sharedLayoutField -and -not [string]::IsNullOrWhiteSpace($sharedLayoutField.Value)) {
return $sharedLayoutField.Value
}
# 4.3 Neither versioned nor shared exist on item => check Standard Values
$template = $item.Template
if (-not $template) {
Write-Error "? Item has no template. Cannot retrieve standard values."
return $null
}
$stdValuesItem = $template.StandardValues
if (-not $stdValuesItem) {
Write-Error "? Could not find Standard Values for template: $($template.Name)"
return $null
}
# 4.3.1 Check versioned in Standard Values (rarely used; usually blank)
$stdFinalField = $stdValuesItem.Fields["__Final Renderings"]
if ($stdFinalField -and -not [string]::IsNullOrWhiteSpace($stdFinalField.Value)) {
return $stdFinalField.Value
}
# 4.3.2 Then check shared in Standard Values
$stdSharedField = $stdValuesItem.Fields["__Renderings"]
if ($stdSharedField -and -not [string]::IsNullOrWhiteSpace($stdSharedField.Value)) {
return $stdSharedField.Value
}
# 4.4 If we reach here, nothing was defined anywhere
Write-Warning "?? No layout XML found on item or its standard values."
return $null
}
# 5. GET AND OUTPUT THE FINAL LAYOUT XML FOR THE PAGE ITEM
$layoutXml = Get-FinalLayoutXml -item $pageItem
if ($layoutXml) {
# 5.1 Print to console
Write-Host "?? Final Layout XML for item '$($pageItem.Paths.FullPath)':`n" -ForegroundColor Green
Write-Output $layoutXml
# 5.2 OPTIONALLY: Uncomment to save to a file instead
# $outFile = "C:\Temp\AAA_FinalLayout.xml"
# $layoutXml | Out-File -FilePath $outFile -Encoding UTF8
# Write-Host "? Layout XML written to: $outFile"
}
else {
Write-Error "? Failed to retrieve any layout XML for '$($pageItem.Paths.FullPath)'."
}
}
Stop-ScriptSession -Session $session
Write a PowerShell script that retrieves a Final layout for a page item (it could be a combination of shared, partial and presentation coming from the template's standard values), I need to retrieve an XML with all the renderings on a page.
The page is /sitecore/content/Zont/Habitat/Home/AAA
@MartinMiles
Copy link
Author

image

@MartinMiles
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment