Photo by Andrew Neel on Unsplash
PowerShell play on Sitecore SXA partial design..
#1 - When you get stuck in between find out usage and placement of all partial design rendering on pages.
Sitecore SXA is a great plug-and-play tool from Sitecore.
Unlike Sitecore traditional way of setting up pages using templates, layouts, renderings and placeholders. SXA uses an additional layer to structure the page using partial design, page design and rendering variants.
I promise not to deep dive further but rather to come on point as Sitecore always provides beautiful documentation - Introducing Sitecore Experience Accelerator
So you got stuck at a point where someone from the Analytics team, or Managers/BA or someone from the Quality team has asked you "Hey I wonder if you can give me the list of all components we are using on our pages"
Being a Sitecore developer you will quickly provide reports by running some Powershell script on the 'Final Layout' of all your content items on the site.
The problem arises when your pages are made up of a combination of traditional Sitecore layouts, renderings as well as SXA partial and page designs.
When you have complex/confusing partial and page designs it is not always a piece of cake to find out in bulk which partials are placed on what pages.
Below powershell script can help you to find partial designs and their renderings on a specific page or all the pages on your site :
#Returns the partial design items of the page
$items = Get-ChildItem -recurse -Path "master:/sitecore/content/[Replace it with site path or specific item]"
$Exportresults = @();
foreach($pageItem in $items)
{
$setdevice = Get-LayoutDevice -Default
$hasLayout = Get-Rendering -Item $pageItem -Device $setdevice -FinalLayout
if($hasLayout.ItemID -ne $null)
{
$instance = [Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider
$presentation = $instance.GetType().GetMethod('GetService').Invoke($instance, [Sitecore.XA.Foundation.Presentation.IPresentationContext])
$partialDesigns = $presentation.GetDesignItem($pageItem)["PartialDesigns"].Split("|", [System.StringSplitOptions]::RemoveEmptyEntries)
$lang = $pageItem.Language.name
foreach ($partial in $partialDesigns) {
if (-not([string]::IsNullOrWhiteSpace($partial))) {
$partialPageItem = Get-Item -Id $partial -Path master: -lang $lang
$setdevice = Get-LayoutDevice -Default
$renderingsList = Get-Rendering -Item $partialPageItem -Device $setdevice -FinalLayout
foreach($renderings in $renderingsList)
{
if($renderings -ne $null)
{
$try = Get-Item $renderings.ItemID
$Properties = @{
PartialDesignAssigned = $partialPageItem.Name
RenderingItemName = $try.DisplayName
UsedOnPage = $pageItem.Name
UsedOnPagePath = $pageItem.Paths.Path
PageTemplate = $pageItem.TemplateName
}
}
$Exportresults += New-Object psobject -Property $Properties
Write-Host "Item:" PartialDesignAssigned
}
}
}
}
}
$Exportresults | Select-Object PartialDesignAssigned,RenderingItemName,UsedOnPage,UsedOnPagePath,PageTemplate| Show-ListView
Once you execute this scripts in Sitecore PowerShell ISE
You will see a nice report that you can export to any file extension provided as per your requirement.
Explanation of generated report:
PartialDesignAssigned: Partial design used on Page item 'sort-things'
RenderingItemName: Renderings assigned in Presentation-Details -> Final Layout of Partial design [Header, Base Page].
UsedOnPage: Partial design and renderings used on the page.
UsedOnPagePath: Path of Item where partial design has been used.
PageTemplate: Template of page item.
You can add more information to your report by modifying this section of the PowerShell script:
$Properties = @{
PartialDesignAssigned = $partialPageItem.Name
RenderingItemName = $try.DisplayName
UsedOnPage = $pageItem.Name
UsedOnPagePath = $pageItem.Paths.Path
PageTemplate = $pageItem.TemplateName
}
Thanks and Happy Sitecoring !!