Skip to content

Instantly share code, notes, and snippets.

@AlexKasaku
Created April 16, 2024 14:41
Show Gist options
  • Save AlexKasaku/5690ce4e69890044971a2642d3e07c3c to your computer and use it in GitHub Desktop.
Save AlexKasaku/5690ce4e69890044971a2642d3e07c3c to your computer and use it in GitHub Desktop.
Find Sitecore items by ancestor template
# Searches for Sitecore items in Master database that descend from a given template, sit
# underneath a particular ancestor item, and are the latest version.
class TemplatesSearchResultItem : SearchResultItem
{
# For items contained within an SXA index try using the name "inheritance_sm".
[Sitecore.ContentSearch.IndexField("_templates")]
[System.Collections.Generic.List[ID]]$TemplateIds
[Sitecore.ContentSearch.IndexField("_latestversion")]
[bool]$IsLatestVersion
}
$rootItem = [ID]::Parse("{DD423562-0AFE-4745-A0BF-F853D180CE5F}")
$templateId = [ID]::Parse("{8812E8B0-AE25-44C2-B0FF-6EE3D1656F25}")
$props = @{
Index = "sitecore_master_index"
Where = "IsLatestVersion == @0 And Paths.Contains(@1) And TemplateIds.Contains(@2)"
WhereValues = $True, $rootItem, $templateId
QueryType = [TemplatesSearchResultItem]
}
Find-Item @props | Initialize-Item | %{
Write-Host $_.Paths.FullPath
%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment