Last active
June 9, 2025 21:32
-
-
Save MartinMiles/0581087cf3cb1beb76edf0014a193c1a 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
| # Creates rendering definition item under a specified ID, in this case it is: {781C6B36-4B02-46EA-8ADC-B92A11877F10} | |
| # 1. Define the full path for the rendering | |
| $FullPath = "/sitecore/layout/Renderings/Feature/Zont/Teasers/Content Teaser with Summary" | |
| # 2. Mount the master: PSDrive if it’s not already mounted | |
| 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 | |
| $db = [Sitecore.Configuration.Factory]::GetDatabase("master") | |
| $basePath = "/sitecore/layout/Renderings" | |
| $parent = $db.GetItem($basePath) | |
| if (-not $parent) { | |
| throw "❌ Base path '$basePath' not found in master database." | |
| } | |
| # 4. Parse template IDs and the rendering definition item ID | |
| $folderTemplate = New-Object Sitecore.Data.TemplateID([Sitecore.Data.ID]::Parse("{7EE0975B-0698-493E-B3A2-0B2EF33D0522}")) | |
| $jsonRenderingTemplate = New-Object Sitecore.Data.TemplateID([Sitecore.Data.ID]::Parse("{04646A89-996F-4EE7-878A-FFDBF1F0EF0D}")) | |
| $renderingDefinitionId = [Sitecore.Data.ID]::Parse("{781C6B36-4B02-46EA-8ADC-B92A11877F10}") | |
| # 5. Split the path into folder segments (excluding the final item name) | |
| $segments = ($FullPath.Substring($basePath.Length).TrimStart("/") -split "/") | |
| $folderNames = $segments[0..($segments.Length - 2)] | |
| foreach ($seg in $folderNames) { | |
| $child = $parent.Children | Where-Object { $_.Name -eq $seg } | |
| if (-not $child) { | |
| try { | |
| $child = $parent.Add($seg, $folderTemplate) | |
| Write-Host "📁 Created folder: $($child.Paths.FullPath)" | |
| } | |
| catch { | |
| throw "❌ Failed to create folder '$seg' under '$($parent.Paths.FullPath)': $_" | |
| } | |
| } | |
| $parent = $child | |
| } | |
| # 6. Create the new JSON rendering item under the last folder | |
| $itemName = $segments[-1] | |
| try { | |
| $newItem = $parent.Add($itemName, $jsonRenderingTemplate, $renderingDefinitionId) | |
| Write-Host "✅ Created JSON rendering: $($newItem.Paths.FullPath)" | |
| } | |
| catch { | |
| throw "❌ Failed to create JSON rendering '$itemName': $_" | |
| } | |
| # 7. Begin editing and set fields via the native API | |
| try { | |
| $newItem.Editing.BeginEdit() | |
| $fieldValues = @{ | |
| "componentName" = "ContentTeaserWithSummary" | |
| "Datasource Location" = "/sitecore/content/Habitat/Settings/Datasources/teasers" | |
| "Datasource Template" = "/sitecore/templates/Project/Common/Content Types/Teasers/Teaser" | |
| "Editable" = "" | |
| "Parameters Template" = "{A2A233A1-6701-48A9-B5F8-EFEAB74B655F}" | |
| "Enable Datasource Query" = "1" | |
| } | |
| foreach ($fieldName in $fieldValues.Keys) { | |
| $field = $newItem.Fields[$fieldName] | |
| if ($field) { | |
| $field.Value = $fieldValues[$fieldName] | |
| } | |
| else { | |
| Write-Warning "⚠ Field '$fieldName' not found on template." | |
| } | |
| } | |
| } | |
| catch { | |
| Write-Error "❌ Error editing fields: $_" | |
| [void]$newItem.Editing.CancelEdit() | |
| throw | |
| } | |
| finally { | |
| [void]$newItem.Editing.EndEdit() | |
| } |
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 me an inline Sitecore PowerShell Extensions (SPE) script, ready to paste into Sitecore PowerShell ISE, that does the following in one go: | |
| 1. Define a variable: | |
| $FullPath = "/sitecore/layout/Renderings/Feature/Zont/Teasers/Content Teaser with Summary" | |
| 2. Mount the master: PSDrive if it’s not already mounted, using New-PSDrive -Name master -PSProvider Sitecore -Root '/' -Database 'master' -ErrorAction Stop. | |
| 3. Use [Sitecore.Configuration.Factory]::GetDatabase('master') to get the master database. | |
| 4. Parse the two template IDs and the target item ID into Sitecore.Data.ID objects: | |
| - Folder template: {7EE0975B-0698-493E-B3A2-0B2EF33D0522} | |
| - Json Rendering template: {04646A89-996F-4EE7-878A-FFDBF1F0EF0D} | |
| - Rendering definition item ID: {781C6B36-4B02-46EA-8ADC-B92A11877F10} | |
| Then wrap the two template IDs in New-Object Sitecore.Data.TemplateID(...). | |
| 5. Split $FullPath into segments under the base path /sitecore/layout/Renderings, and for each folder segment up to the final one: | |
| - Check if a child with that name exists under the current parent. | |
| - If it doesn’t, call the native Item.Add(name, TemplateID) overload on the current parent (not SPE cmdlets), log with Write-Host on success, or throw on failure with -ErrorAction Stop. | |
| 6. Under the last folder, create the new rendering definition item by calling the native parent.Add(itemName, TemplateID, itemId) overload, log success or throw on failure. | |
| 7. Open editing with $item.Editing.BeginEdit(), then set these fields via the native API: | |
| - componentName = "ContentTeaserWithSummary" | |
| - Datasource Location = "/sitecore/content/Habitat/Settings/Datasources/teasers" | |
| - Datasource Template = "/sitecore/templates/Project/Common/Content Types/Teasers/Teaser" | |
| - Editable = "" | |
| - Parameters Template = "{A2A233A1-6701-48A9-B5F8-EFEAB74B655F}" | |
| - Enable Datasource Query = "1" | |
| For each, check $item.Fields[name] exists (use Write-Warning if not) before assigning Value. Wrap the whole in a try/catch that calls CancelEdit() on error, and always call [void]$item.Editing.EndEdit() in finally. | |
| 8. Use robust -ErrorAction Stop on all operations so any failure throws, and include clear Write-Host and Write-Warning messages. Do **not** use SPE helpers like Set-ItemField or New-Item -Id. | |
| Output **only** the final PowerShell script block. |
Author
MartinMiles
commented
May 31, 2025

Notes: does not set XMC placeholders into Layout Service Placeholders field
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment