Created
November 5, 2020 19:50
-
-
Save AlexKasaku/d47196d15ff82eb76f69561f8ecbdf39 to your computer and use it in GitHub Desktop.
Sitecore - Fix broken renderings
This file contains 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
# | |
# Recurses through the content and finds all Renderings used by pages. Used to find any broken renderings. | |
# Assumes datasources can only be IDs, so setups that use queries can be flagged as incorrect (e.g. in SXA). | |
# | |
$rootItem = Get-Item -Path "master:/sitecore/content/website/Home" | |
$defaultLayout = (Get-LayoutDevice "Default") | |
# Set to true to only report on issues, rather than address them. If this is false, renderings deemed bad are removed! | |
$reportOnly = $True | |
foreach ( $item in Get-ChildItem -Item $rootItem -Recurse ) | |
{ | |
$renderings = $item | Get-Rendering -Device $defaultLayout -FinalLayout | |
foreach ( $rendering in $renderings ) | |
{ | |
$removeRendering = $False | |
if ($rendering -eq $null) | |
{ | |
Write-Host "$($item.FullPath) - Has null rendering" | |
} | |
else | |
{ | |
if ($rendering.ItemID -eq $null) | |
{ | |
Write-Host "$($item.FullPath) - Has rendering with null ItemID" | |
$removeRendering = $True | |
} | |
else | |
{ | |
$renderingItem = Get-Item -Path master: -ID $rendering.ItemID -ErrorAction SilentlyContinue | |
if ($renderingItem -eq $null) | |
{ | |
# This item is referencing a broken rendering | |
Write-Host "$($item.FullPath) - Has rendering with broken ItemID. ID = $($rendering.ItemID). Data Source = $($rendering.Datasource)" | |
$removeRendering = $True | |
} | |
elseif ($rendering.Datasource) | |
{ | |
$datasourceItem = Get-Item -Path master: -ID $rendering.Datasource -ErrorAction SilentlyContinue | |
if ($datasourceItem -eq $null) | |
{ | |
Write-Host "$($item.FullPath) - Has rendering with broken datasource. Rendering = $($renderingItem.Name) ($($rendering.ItemID)). Placeholder = $($rendering.Placeholder). Data Source = $($rendering.Datasource)" | Format-Wide | |
if (!$reportOnly) | |
{ | |
Write-Host "Removing datasource" | |
$rendering.Datasource = $null | |
Set-Rendering -Item $item -Instance $rendering -FinalLayout | |
} | |
} | |
} | |
} | |
} | |
if ($removeRendering -and !($reportOnly)) | |
{ | |
Write-Host "Removing rendering" | |
Remove-Rendering -Item $item -Instance $rendering -FinalLayout | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment