Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sitecore PowerShell Extensions Script for finding pages where a rendering is used
# 1) Path to Presentation node of your Site
$presentationPath = "/sitecore/content/TennantGroup/Tennant/Shared Website/Presentation"
# 2) IDs for derived Metadata Partial Design template
$metadataPartialDesignTemplateId = "{C546A218-0707-4B3E-905B-EBD866533AE7}"
# 3) ID for derived Partial Design template
$partialDesignTemplateId = "{341CDBE1-FE54-47F5-AAB9-016AC4FA8C0D}"
# 4) ID for derived Page Design template
$pageDesignTemplateId = "{39543BFF-CE95-40D3-9873-E5E5A0E1BDFF}"
# 5) Path for Home page
$homePagePath = "/sitecore/content/TennantGroup/Tennant/Developer Website/Home"
$renderingId = Show-Input "Enter the ID of a rendering:"
$rendering = Get-Item -Path master: -ID $renderingId
$partialDesignsPath = $presentationPath + "/Partial Designs"
$pageDesignsPath = $presentationPath + "/Page Designs"
$sxaPartialDesignTemplateId = "{BF680756-B2FA-4CAE-8B69-EE361080616F}"
$sxaMetadataPartialDesignTemplateId = "{C2A8E276-1757-4957-9DD5-8669B79FD80D}"
$partialDesignsQuery = $partialDesignsPath + "//*[@@templateid='" + $metadataPartialDesignTemplateId + "' or @@templateid='" + $partialDesignTemplateId + "' or @@templateid='" + $sxaPartialDesignTemplateId + "' or @@templateid='" + $sxaMetadataPartialDesignTemplateId + "']"
$partialDesigns = Get-Item -Path master: -Query $partialDesignsQuery
$sxaPageDesignTemplateId = "{EBCFFAE1-B6B4-4B50-B722-155E869FC430}"
$pageDesignsQuery = $pageDesignsPath + "//*[@@templateid='" + $pageDesignTemplateId + "' or @@templateid='" + $sxaPageDesignTemplateId + "']"
$pageDesignsItem = Get-Item -Path master: -Query $pageDesignsPath
$pageDesignsCollection = Get-Item -Path master: -Query $pageDesignsQuery
$sxaTemplatesMappingFieldId = '{BA1F60D6-3DEB-40CC-BB61-EEC772279EE1}'
$decoded = [System.Web.HttpUtility]::UrlDecode([System.Web.HttpUtility]::UrlDecode($pageDesignsItem[$sxaTemplatesMappingFieldId]))
$pageDesignLookup = [System.Web.HttpUtility]::ParseQueryString($decoded)
$referrerPages = @()
foreach ($partialDesign in $partialDesigns) {
#Write-Host $partialDesign.Name
if (($partialDesign["__Renderings"] -match $rendering.ID.ToString()) -or ($partialDesign["__Final Renderings"] -match $rendering.ID.ToString())) {
$message = "Found rendering " + $rendering.Name + " within partial design " + $partialDesign.Name
#Write-Host $message
$pageDesigns = Get-ItemReferrer -Item $partialDesign | Where-Object { $_.TemplateId -eq $pageDesignTemplateId }
#Write-Host " " + $pageDesigns.Name
$itemPaths = @()
$pageDesignIds = @()
$pageDesignNames = @()
foreach ($pageDesign in $pageDesigns) {
$innerMessage = " Partial design " + $partialDesign.Name + " referenced by page design " + $pageDesign.Name
#Write-Host $innerMessage
$pageDesignIds += $pageDesign.ID
$pageDesignNames += $pageDesign.Name
$query = $homePagePath + "//*[@#Page Design#='" + $pageDesign.ID + "']"
$page = Get-Item -Path master: -Query $query | Select -First 1
if ($page -eq $null) {
#Write-Host " null item encountered"
#Write-Host $pageDesignLookup.AllKeys
foreach ($key in $pageDesignLookup.AllKeys) {
#Write-Host $key
if ($pageDesignLookup[$key] -eq $pageDesign.ID.ToString()) {
$pageTemplate = Get-Item -Path master: -ID $key
$pages = Get-ItemReferrer -Item $pageTemplate | Where-Object { $_.Paths.IsContentItem -and $_.TemplateId -eq $pageTemplate.ID }
#Write-Host $pages.Length
#Write-Host $pages[0].Paths.FullPath
#Write-Host $pages[1].Paths.FullPath
}
}
}
$resultObject = [PSCustomObject]@{
RenderingID = $rendering.ID
RenderingName = $rendering.Name
PartialDesignID = $partialDesign.ID
PartialDesignName = $partialDesign.Name
PageDesignID = $pageDesign.ID
PageDesignName = $pageDesign.Name
ItemPath = $page.Paths.FullPath
}
$referrerPages += $resultObject
}
}
}
$dialogTitle = "Pages using the '" + $rendering.Name + "' rendering."
$referrerPages | Show-ListView -InfoTitle $dialogTitle
@dresser

This comment has been minimized.

Copy link
Owner Author

@dresser dresser commented Nov 29, 2020

This script can be copied and pasted into a new script window in the PowerShell ISE interface in Sitecore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment