Continuing on this series of a useful Sitecore PowerShell scripts that you will need while working on a Sitecore Audit, in this post we will discuss how you should not have excessive use of rendering variants, For Sitecore SXA best practices, it's recommended to limit the number of rendering variants to 15 or preferably below 10 to maintain a manageable and efficient development process. will explain in this post why? and if you're performing a sitecore audit, how you can catch that with simple PowerShell script, keep reading!!
A large number of variants can make it difficult for developers and designers to manage and maintain the codebase, leading to potential errors and increased development time. Instead of creating numerous variants, focus on creating a flexible set of core renderings and variants that can be adapted to different needs. If you have multiple sites within a Sitecore tenant, consider defining rendering variants in a shared site and making them available to all other sites in that collection.
Following is a PowerShell script that will go through renderings variants and find the excessive use of variants for you:
# Function to find renderings with more than 10 variants | |
function Get-RenderingsWithManyVariants { | |
param ( | |
[Parameter(Mandatory=$true)] | |
[Sitecore.Data.Items.Item]$variantsFolder | |
) | |
# Initialize an empty array to store results | |
$renderingsWithManyVariants = @() | |
if ($variantsFolder) { | |
# Get all variants under the folder | |
$variants = $variantsFolder.Children | |
# Check if the number of variants exceeds 10 | |
if ($variants.Count -gt 10) { | |
# Add the rendering to the result if it has more than 10 variants | |
$renderingsWithManyVariants += New-Object PSObject -Property @{ | |
RenderingName = $variantsFolder.Name | |
RenderingPath = $variantsFolder.Paths.FullPath | |
VariantCount = $variants.Count | |
} | |
write-host $renderingsWithManyVariants | |
} | |
} | |
return $renderingsWithManyVariants | |
} | |
# Get the root folder where renderings are stored (usually /sitecore/layout/Renderings or under /sitecore/content) | |
$renderingsRoot = Get-Item -Path "master:/sitecore/content/Tenant/Website/Presentation/Rendering Variants" | |
# Initialize an empty array to store results | |
$renderingsWithMoreThan10Variants = @() | |
# Function to recursively traverse all renderings and check for number of variants | |
function Traverse-Renderings { | |
param ( | |
[Sitecore.Data.Items.Item]$item | |
) | |
# Check if the current item is a rendering (usually with a template like 'Rendering') | |
if ($item.TemplateName -eq "Variants") { | |
# Check if the rendering has more than 10 variants | |
$renderingVariantsCheck = Get-RenderingsWithManyVariants -variantsFolder $item | |
if ($renderingVariantsCheck.Count -gt 0) { | |
$renderingsWithMoreThan10Variants += $renderingVariantsCheck | |
} | |
} | |
# Recursively check child items | |
foreach ($child in $item.Children) { | |
Traverse-Renderings -item $child | |
} | |
} | |
# Start traversing from the renderings root item | |
Traverse-Renderings -item $renderingsRoot | |
# Output the results | |
if ($renderingsWithMoreThan10Variants.Count -gt 0) { | |
$renderingsWithMoreThan10Variants | Format-Table -AutoSize | |
} else { | |
Write-Host "No renderings found with more than 10 variants." | |
} |
Hopefully the above helps you on your Audit, and make it easier and faster to verify this point, happy auditing!
No comments:
Post a Comment