Created
August 1, 2024 02:44
-
-
Save bala-one/e938db8278e347ae4d29a6aa208009dd to your computer and use it in GitHub Desktop.
MVC to Headless Content Updates with Sitecore Powershell
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
################################################### | |
# Update the following parameters before execution | |
################################################### | |
#Starting path in content tree | |
$startingPath = "master:\content\home" | |
$defaultLayoutPath = 'master:\layout\mysite\Default' | |
#Report only and not update any item | |
$reportOnly = $False; | |
$useFinalLayout = $True; | |
#Placeholder key old to new mapping | |
$placeholderMappings = @( | |
@("main","page"), | |
@("/main","/page"), | |
@("/page content","/new content") | |
) | |
#Renderings id with name mapping | |
$renderingMappings = @( | |
@("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}","{yyyyyyyy-xxxx-xxxx-xxxx-xxxxxxxxxxxx}","Old component","New component") | |
) | |
# Get all items in the path | |
$items = Get-ChildItem -Path $startingPath -Recurse | Where-Object { (Get-Rendering -Item $_ -FinalLayout) -ne $null } | |
# Loop through each item | |
foreach ($item in $items) { | |
# select default layout | |
$defaultDevice = Get-LayoutDevice -Default | |
$mobileDevice = Get-LayoutDevice -Name 'Mobile' | |
# change the layout from what is in Standard values to the new one. | |
$defaultLayout = Get-Item -Path $defaultLayoutPath | |
if (!$reportOnly) | |
{ | |
Set-Layout -Item $item -Device $defaultDevice -Layout $defaultLayout | Out-Null | |
} | |
Write-Host "Default Layout changed for $($item.Paths.Path)" | |
$defaultPh = Get-PlaceholderSetting -Item $item -Key "main*" -Device $defaultDevice -FinalLayout | |
if($null -ne $defaultPh){ | |
if (!$reportOnly){ | |
Remove-PlaceholderSetting -Item $item -Key "main*" -Device $defaultDevice -FinalLayout | |
} | |
Write-Host "Placeholder Main removed from default layout" | |
} | |
$mobileMainPh = Get-PlaceholderSetting -Item $item -Key "main*" -Device $mobileDevice -FinalLayout | |
if($null -ne $mobileMainPh) | |
{ | |
if (!$reportOnly){ | |
Remove-PlaceholderSetting -Item $item -Key "main*" -Device $mobileDevice -FinalLayout | |
} | |
Write-Host "Placeholder Main removed from Mobile layout" | |
} | |
$mobilepcPh = Get-PlaceholderSetting -Item $item -Key "pagecontainer*" -Device $mobileDevice -FinalLayout | |
if($null -ne $mobilepcPh) | |
{ | |
if (!$reportOnly){ | |
Remove-PlaceholderSetting -Item $item -Key "pagecontainer*" -Device $mobileDevice -FinalLayout | |
} | |
Write-Host "Placeholder pagecontainer removed from Mobile layout" | |
} | |
## Replace Renderings with mappings | |
foreach( $renderMapping in $renderingMappings ) | |
{ | |
$sourceRenderingId = $renderMapping[0]; | |
$targetRenderingId = $renderMapping[1]; | |
$sourceRenderings = Get-Rendering -Item $item -Device $defaultDevice -FinalLayout | Where-Object { $_.ItemID -eq $sourceRenderingId } | |
$targetRendering = Get-Item -Path "master:" -ID $targetRenderingId | New-Rendering | |
# Replace all instances of the old rendering with the new rendering | |
foreach($sourceRendering in $sourceRenderings) { | |
if (!$reportOnly) | |
{ | |
$item | Switch-Rendering -Instance $sourceRendering -Device $defaultDevice -FinalLayout -NewRendering $targetRendering | |
} | |
Write-Host "$($item.FullPath) - Switching Rendering : $($renderMapping[2]) --> $($renderMapping[3])" | |
} | |
} | |
## Replace Placeholders with mappings | |
foreach( $mapping in $placeholderMappings ) | |
{ | |
# Get renderings in this item that have renderings in the placeholder we want to update | |
$renderings = Get-Rendering -Item $item -Placeholder ($mapping[0] + '*') -Device $defaultDevice -FinalLayout:$useFinalLayout | |
foreach ( $rendering in $renderings ) | |
{ | |
# Only update the rendering if we're not in "Report Only" mode | |
if (!$reportOnly) | |
{ | |
# Update the placeholder in the rendering and set it back in the item | |
$rendering.Placeholder = $rendering.Placeholder -replace $mapping[0], $mapping[1] | |
Set-Rendering -Item $item -Instance $rendering -FinalLayout:$useFinalLayout | |
} | |
Write-Host "$($item.FullPath) - Rendering $($rendering.UniqueID) - Placeholder: $($mapping[0]) --> $($mapping[1])" | |
} | |
} | |
} | |
Write-Host "Script execution completed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment