Last active
June 6, 2025 23:00
-
-
Save MartinMiles/2433d9637ffc91f4be752ff19813cac5 to your computer and use it in GitHub Desktop.
Create a Json rendering and also an XM Cloud compatible rendering parameters template for it. Inherit existing parameters templates
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
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 | |
} | |
# 2. Get master database | |
$db = [Sitecore.Configuration.Factory]::GetDatabase("master") | |
if (-not $db) { Throw "Cannot get master database." } | |
# 3. Ensure rendering folder structure exists | |
$renderingPath = "/sitecore/layout/Renderings/Feature/Zont/Teasers/Content Teaser with Summary" | |
$relative = $renderingPath.Substring("/sitecore/layout/Renderings/".Length) | |
$segments = $relative.Split('/') | |
$parent = $db.GetItem("/sitecore/layout/Renderings") | |
if (-not $parent) { Throw "Missing /sitecore/layout/Renderings" } | |
for ($i = 0; $i -lt $segments.Length - 1; $i++) { | |
$name = $segments[$i] | |
$child = $parent.Children[$name] | |
if (-not $child) { | |
$child = $parent.Add( | |
$name, | |
[Sitecore.Data.TemplateID]::new([Sitecore.Data.ID]::Parse("{7EE0975B-0698-493E-B3A2-0B2EF33D0522}")) | |
) | |
Write-Host "Created folder: $($child.Paths.FullPath)" | |
} | |
$parent = $child | |
} | |
# 4. Create or reuse rendering definition item | |
$itemName = $segments[-1] | |
$renderingItem = $db.GetItem($renderingPath) | |
if (-not $renderingItem) { | |
$renderingItem = $parent.Add( | |
$itemName, | |
[Sitecore.Data.TemplateID]::new([Sitecore.Data.ID]::Parse("{04646A89-996F-4EE7-878A-FFDBF1F0EF0D}")) | |
) | |
Write-Host "Created rendering definition: $($renderingItem.Paths.FullPath)" | |
} else { | |
Write-Host "Rendering definition exists: $renderingPath" | |
} | |
# 5. Ensure “Rendering Parameters” folder under templates | |
$templateFolderParts = "Feature","Zont","Habitat","Teasers","Rendering Parameters" | |
$current = $db.GetItem("/sitecore/templates") | |
if (-not $current) { Throw "Missing /sitecore/templates" } | |
foreach ($part in $templateFolderParts) { | |
$child = $current.Children[$part] | |
if (-not $child) { | |
$child = $current.Add( | |
$part, | |
[Sitecore.Data.TemplateID]::new([Sitecore.Data.ID]::Parse("{0437FEE2-44C9-46A6-ABE9-28858D9FEE8C}")) | |
) | |
Write-Host "Created template folder: $($child.Paths.FullPath)" | |
} | |
$current = $child | |
} | |
# 6. Create or reuse the parameters template item | |
$templatePath = "$($current.Paths.FullPath)/$itemName" | |
$paramTemplate = $db.GetItem($templatePath) | |
if (-not $paramTemplate) { | |
$paramTemplate = $current.Add( | |
$itemName, | |
[Sitecore.Data.TemplateID]::new([Sitecore.Data.ID]::Parse("{AB86861A-6030-46C5-B394-E8F99E8B87DB}")) | |
) | |
Write-Host "Created parameters template: $($paramTemplate.Paths.FullPath)" | |
} else { | |
Write-Host "Parameters template exists: $templatePath" | |
} | |
# 7. Build five-GUID base-templates string | |
$baseGuids = @( | |
"{5C74E985-E055-43FF-B28C-DB6C6A6450A2}", | |
"{44A022DB-56D3-419A-B43B-E27E4D8E9C41}", | |
"{3DB3EB10-F8D0-4CC9-BE26-18CE7B139EC8}", | |
"{4247AAD4-EBDE-4994-998F-E067A51B1FE4}" | |
) | |
if ($ParametersTemplateId) { $baseGuids += $ParametersTemplateId } | |
$joined = $baseGuids -join "|" | |
# 8. Set shared “__Base template” field by ID | |
$baseFieldId = [Sitecore.Data.ID]::Parse("{12C33F3F-86C5-43A5-AEB4-5598CEC45116}") # __Base template | |
# Reload to ensure fresh | |
$paramTemplate = $db.GetItem($templatePath) | |
$paramTemplate.Editing.BeginEdit() | |
$paramTemplate.Fields[$baseFieldId].Value = $joined | |
$paramTemplate.Editing.EndEdit() | |
# Verify | |
$paramTemplate = $db.GetItem($templatePath) | |
if ($paramTemplate.Fields[$baseFieldId].Value -ne $joined) { | |
Throw "Failed to set __Base template. Current: '$($paramTemplate.Fields[$baseFieldId].Value)'" | |
} | |
Write-Host "__Base template set to: $($paramTemplate.Fields[$baseFieldId].Value)" | |
# 9. Link this parameters template in the rendering definition item | |
if ($renderingItem.Fields["Parameters Template"]) { | |
$renderingItem.Editing.BeginEdit() | |
$renderingItem.Fields["Parameters Template"].Value = $paramTemplate.ID.ToString() | |
$renderingItem.Editing.EndEdit() | |
Write-Host "Linked Parameters Template: $($paramTemplate.Paths.FullPath)" | |
} else { | |
Write-Warning "Rendering definition missing 'Parameters Template' field." | |
} | |
# 10. Set standard rendering-definition fields | |
$renderingItem.Editing.BeginEdit() | |
if ($renderingItem.Fields["componentName"]) { $renderingItem.Fields["componentName"].Value = "ContentTeaserWithSummary" } | |
if ($renderingItem.Fields["Datasource Location"]) { $renderingItem.Fields["Datasource Location"].Value = "/sitecore/content/Habitat/Settings/Datasources/teasers" } | |
if ($renderingItem.Fields["Datasource Template"]) { $renderingItem.Fields["Datasource Template"].Value = "/sitecore/templates/Project/Common/Content Types/Teasers/Teaser" } | |
if ($renderingItem.Fields["Editable"]) { $renderingItem.Fields["Editable"].Value = "" } | |
if ($renderingItem.Fields["Enable Datasource Query"]) { $renderingItem.Fields["Enable Datasource Query"].Value = "1" } | |
$renderingItem.Editing.EndEdit() | |
Write-Host "All done for rendering: $($renderingItem.Paths.FullPath)" |
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
You are a world-renowned Sitecore XM Cloud scripting expert. Write an inline Sitecore PowerShell Extensions (SPE) script, ready to paste into Sitecore PowerShell ISE, that does exactly the following in one go: | |
1. Declare a parameter: | |
```powershell | |
param( | |
[string]$ParametersTemplateId = "{A2A233A1-6701-48A9-B5F8-EFEAB74B655F}" | |
)``` | |
Use -ErrorAction Stop globally so any failure throws immediately. | |
Mount the master: PSDrive if it’s not already mounted. | |
Get the master database via [Sitecore.Configuration.Factory]::GetDatabase("master") and throw if null. | |
Define: | |
powershell | |
Copy | |
Edit | |
$FullPath = "/sitecore/layout/Renderings/Feature/Zont/Teasers/Content Teaser with Summary" | |
Split this path into segments. | |
Under /sitecore/layout/Renderings, recreate any missing folders using the Rendering Folder template ID {7EE0975B-0698-493E-B3A2-0B2EF33D0522}, logging each new folder with Write-Host. | |
Create or reuse the rendering definition item: | |
Template ID for Json Rendering: {04646A89-996F-4EE7-878A-FFDBF1F0EF0D}. | |
Log creation or existence. | |
Under /sitecore/templates/Feature/Zont/Habitat/Teasers/Rendering Parameters, recreate any missing folders using Template Folder ID {0437FEE2-44C9-46A6-ABE9-28858D9FEE8C}. | |
Create or reuse the Rendering Parameters template item named “Content Teaser with Summary” based on the Template template ID {AB86861A-6030-46C5-B394-E8F99E8B87DB}, logging creation or existence. | |
Set its shared __Base template field (field ID {12C33F3F-86C5-43A5-AEB4-5598CEC45116}) to a pipe-separated list of these five GUIDs in this exact order: | |
{5C74E985-E055-43FF-B28C-DB6C6A6450A2} | |
{44A022DB-56D3-419A-B43B-E27E4D8E9C41} | |
{3DB3EB10-F8D0-4CC9-BE26-18CE7B139EC8} | |
{4247AAD4-EBDE-4994-998F-E067A51B1FE4} | |
$ParametersTemplateId (if provided) | |
Assign that template’s ID to the rendering definition item’s Parameters Template field. | |
Begin editing the rendering definition and set its fields via the API (not SPE cmdlets): | |
componentName = "ContentTeaserWithSummary" | |
Datasource Location = "/sitecore/content/Habitat/Settings/Datasources/teasers" | |
Datasource Template = "/sitecore/templates/Project/Common/Content Types/Teasers/Teaser" | |
Editable = "" | |
Enable Datasource Query = "1" | |
Use [void]$item.Editing.EndEdit() or EndEdit() to suppress return values. | |
Use only native Sitecore API calls (GetDatabase, Add(), item indexer for fields, BeginEdit()/EndEdit()) and avoid SPE helper cmdlets like Set-ItemField or passing -Id to New-Item. | |
Write Write-Host messages at each major step to confirm folder creation, item creation, field setting, and final success. | |
Output only the final PowerShell script block. |
Author
MartinMiles
commented
Jun 6, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment