Skip to content

Instantly share code, notes, and snippets.

View MartinMiles's full-sized avatar

Martin Miles MartinMiles

View GitHub Profile
<#
.SYNOPSIS
Download a page and parse Sitecore debug comments into a JSON hierarchy.
.DESCRIPTION
Scans a Sitecore-rendered HTML page for <!-- start-component='…' --> and <!-- end-component='…' -->
markers, extracts metadata (name, id, uid, placeholder, path), and reconstructs a nested
component-to-placeholder tree. Strict UID matching ensures proper pairing of start/end tags,
while allowing the root layout to remain on the stack at the end.
<#
.SYNOPSIS
Lists every item (and its template's Standard Values) that actually has
presentation details (shared and/or final layouts) beneath a chosen site root.
.PARAMETER SiteRoot
The content path to start from. Defaults to "/sitecore/content/Zont/Habitat/Home".
.PARAMETER DatabaseName
The Sitecore database to query (master/web/old/etc.). Defaults to "master".
param(
[string] $PlaceholderKey = "col-wide-2"
)
# 1) Load master database
$db = [Sitecore.Configuration.Factory]::GetDatabase("master")
if (-not $db) {
Write-Error "ERROR: Could not load the 'master' database."
exit 1
}
@MartinMiles
MartinMiles / Get-LayoutServicePlaceholderIDs-ForXmCloudRenderings-REMOTING.ps1
Last active June 9, 2025 10:57
We need to find which nested placeholders are defined within renderings on a page, this script does it (say, for `ArticleAsideLeft` it returns `col-wide-1-0-1`). We need this later to set `Layout Service Placeholders` field on XM Cloud renderings
Set-Location -Path $PSScriptRoot
# Load connection settings
$config = Get-Content -Raw -Path ./config.LOCAL.json | ConvertFrom-Json
# Import SPE and start a remote session
Import-Module -Name SPE
$session = New-ScriptSession -ConnectionUri $config.connectionUri `
-Username $config.username `
-SharedSecret $config.SPE_REMOTING_SECRET
$ErrorActionPreference = 'Stop'
# ❶ Mount master: drive if absent
if (-not (Get-PSDrive -Name master -ErrorAction SilentlyContinue)) {
New-PSDrive -Name master -PSProvider Sitecore -Root "/" -Database "master" -ErrorAction Stop | Out-Null
}
# ❷ Get master database
$db = [Sitecore.Configuration.Factory]::GetDatabase('master')
if (-not $db) { throw 'Cannot get master database.' }
@MartinMiles
MartinMiles / Create-SingleJsonRenderingWithParametersTemplate.ps1
Last active June 6, 2025 23:00
Create a Json rendering and also an XM Cloud compatible rendering parameters template for it. Inherit existing parameters templates
param(
[string]$ParametersTemplateId = "{A2A233A1-6701-48A9-B5F8-EFEAB74B655F}"
)
$ErrorActionPreference = "Stop"
# 1. Mount master drive if needed
if (-not (Get-PSDrive -Name master -ErrorAction SilentlyContinue)) {
New-PSDrive -Name master -PSProvider Sitecore -Root "/" -Database "master" -ErrorAction Stop | Out-Null
}
@MartinMiles
MartinMiles / Slice-HtmlToComponents.ps1
Last active June 4, 2025 22:07
Runs on a host machine, pulls a web page from legacy site, slices components into react/next.js tsx files under the same paths/subfolders as was done for cshtml, pulls the components hierarchy and sets placeholders as per original names
<#
.SYNOPSIS
Slices a fully rendered Sitecore HTML page into individual Next.js component `.tsx` files,
based on the <!-- start-component='…' --> / <!-- end-component='…' --> markers and
by calling Get-Layout.ps1 (which now returns JSON) for the layout hierarchy.
.PARAMETER Url
The URL of the rendered page. Defaults to http://rssbplatform.dev.local/aaa
.PARAMETER ItemPath
@MartinMiles
MartinMiles / Get-PlaceholdersToComponentsLayoutJson-REMOTING.ps1
Created June 4, 2025 20:03
Used to help mapping rederings to the placeholders
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)"
# -----------------------------------------------
# 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.
# -----------------------------------------------
@MartinMiles
MartinMiles / Rebind-TopLevelPlaceholders.ps1
Last active June 15, 2025 08:41
Processes all the rendering for a page and rebinds a top level component from one to another (say, from `page-layout` to `headless-main`) including those nested
# -----------------------------------------------------------
# Inline Script for Sitecore PowerShell ISE:
# Replace Multiple Placeholders for Renderings
# Target Item: Default is /sitecore/content/Zont/Habitat/Home/AAA
# DB: master
# - Processes Standard Values, Shared Layout, and Final Layout.
# - Replaces three placeholder pairs:
# $OldPlaceholder1 → $NewPlaceholder1
# $OldPlaceholder2 → $NewPlaceholder2
# $OldPlaceholder3 → $NewPlaceholder3