Last active
December 17, 2018 23:37
-
-
Save fluxdigital/b8217d9b159347af52da848bf53d5c68 to your computer and use it in GitHub Desktop.
SPE script to swaps one rendering for another and also deletes an rendering if required and updates datasources
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
$rootItem = Get-Item -Path "/sitecore/templates/Site1/Pages" #update to the path with your page templates | |
$deviceLayout = Get-LayoutDevice "Default" #update to device layout you wish to target | |
$templateMode = $True #set to False to update pages instead of templates (usually you will want to update template standard values) | |
$useFinalLayout = $False #set to True to use FinalRenderings instead of Renderings field | |
$checkForLayout = $False #set to True to skip items with no layout (use only for updating pages and not templates) | |
$updateDataSourcesOnly = $False #set to True to only update datasources and not swap or delete renderings | |
$reportMode = $False #set to True to just output info on renderings to be swapped/deleted/updated | |
$modeType = "" | |
$placeholder = "main" #update to use the placeholder your renderings exist in | |
$renderingIdToSwap = "{15BCE4DA-4F3C-42CA-9D4D-D46D572C7A8F}" #update to the rendering id you wish to swap out | |
$renderingIdToAdd = "{474FF789-1D6B-44E8-AFF9-C666A400EA0F}" #update to the rendering id you wish to swap in | |
$renderingToAdd = Get-Item -Path "$($renderingIdToAdd)" | |
$renderingDataSourceToAdd = "{7352EA16-D488-4B89-BAF5-497A24A40C6F}" #update to the datasource you wish to use for your rendering (set to $null to skip updating a datasource) | |
$renderingIdToRemove = "{A8950047-56E4-4793-A5E6-AA892236B2FF}" #update to a rendering id you wish to remove | |
# List of template ids to exclude from processing | |
$excludedTemplateIds = @( | |
"{E269FBB5-3750-427A-9149-7AA950B49301}", #field section | |
"{455A3E98-A627-4B40-8035-E683A0331AC7}" #field | |
) | |
$global:pagesFoundCount = 0 | |
$global:updatedCount = 0 | |
$global:removedCount = 0 | |
$global:dsupdatedCount = 0 | |
if($templateMode){ | |
$modeType = "Template" | |
} | |
else{ | |
$modeType = "Page" | |
} | |
#get Pages/Templates below root item path to update (if updating templates then get their standard values to update) | |
Write-Host "Getting $($modeType)s below: $($rootItem.FullPath)" | |
Get-ChildItem -Item $rootItem -Recurse | Where-Object {$_.TemplateName -ne "__Standard Values" -and $excludedTemplateIds -notcontains $_.TemplateId} | ForEach-Object { | |
#template mode | |
if($templateMode){ | |
$templateToUpdate = $_ | |
if(Test-Path -Path "master:$($templateToUpdate.FullPath)/__Standard Values"){ | |
$templateStandardValues = Get-Item -Path "master:$($templateToUpdate.FullPath)/__Standard Values" | |
Update-Page-Renderings -parentItem $templateToUpdate -item $templateStandardValues | |
} | |
} | |
#page mode | |
else{ | |
$pageToUpdate = $_ | |
Update-Page-Renderings -parentItem null -item $pageToUpdate | |
} | |
} | |
Write-Host "---Script Complete---" | |
Write-Host "Total $($modeType) processed: $($global:pagesFoundCount)" | |
Write-Host "$($modeType) with renderings swapped & datasources updated: $($global:updatedCount)" | |
Write-Host "$($modeType) with renderings removed: $($global:removedCount)" | |
Write-Host "$($modeType) with datasources only updated: $($global:dsupdatedCount)" | |
function Update-Page-Renderings($parentItem, $item, $checkForLayout){ | |
# Only process pages with a layout (we ignore this for templates) | |
if ((Get-Layout $item) -or !$checkForLayout) | |
{ | |
$global:pagesFoundCount ++ | |
Write-Host "- Updating $($modeType) $($pagesFoundCount): $($parentitem.Name) - $($item.Name) -" | |
# Get renderings in this page and loop over them | |
$renderings = Get-Rendering -Item $item -Placeholder ($placeholder) -Device $deviceLayout -FinalLayout:$useFinalLayout | |
foreach ($rendering in $renderings) | |
{ | |
$renderingItem = Get-Item -Path $rendering.ItemID | |
Write-Host "Checking Rendering: $($rendering.UniqueID) - '$($renderingItem.Name)' in Placeholder: $($placeholder) for Item: $($item.Name)" | |
$swapRendering = $renderingIdToSwap -eq $rendering.ItemID | |
$removeRendering = $renderingIdToRemove -eq $rendering.ItemID; | |
$updateRenderingDs = $updateDataSourcesOnly -and $renderingIdToAdd -eq $rendering.ItemID | |
if(!$reportMode){ | |
# if the rendering id matches the one we want to swap out then update it to the new rendering and set a new datasource too | |
if($swapRendering){ | |
Swap-Rendering -item $item -renderingToSwapOut $rendering -placeholder $placeholder ` | |
-renderingIdToAdd $renderingIdToAdd -renderingDataSourceIdToAdd $renderingDataSourceToAdd -useFinalLayout $useFinalLayout | |
$global:updatedCount ++ | |
} | |
# if the rendering id matches the one we want to remove then remove it | |
if($removeRendering){ | |
Write-Host "Removing Rendering: $($renderingIdToRemove) for Item $($item.Name)" | |
Remove-Rendering -Item $item -Instance $rendering -FinalLayout:$useFinalLayout | |
$global:removedCount ++ | |
} | |
# set new datasources only | |
if($updateRenderingDs){ | |
Swap-DataSource -item $item -renderingToUpdate $rendering -renderingDataSourceIdToAdd $renderingDataSourceToAdd -useFinalLayout $useFinalLayout | |
$global:dsupdatedCount ++ | |
} | |
} | |
else{ | |
Write-Host "REPORT MODE ON - Swap: $($swapRendering) ~ Remove: $($removeRendering) ~ Update DataSource Only: $($updateRenderingDs)" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment