Powershell get all items that use a template












4














The title pretty much says it all - I've looked at the docs but I just keep finding Get-Item and Get-ItemTempate.



Lets say I have a template with id 123456, I want to get all items that are using this template. How would I do that?










share|improve this question






















  • How would you do that in C#?
    – Alan Płócieniak
    Nov 26 at 15:25










  • Something like allItems.Where(p => p.TemplateIdList.Contains("123456")); Let me see if I can powershell that! - Disclaimer - New to Sitecore so I don't know what exists and what doesn't...
    – Rick
    Nov 26 at 15:33


















4














The title pretty much says it all - I've looked at the docs but I just keep finding Get-Item and Get-ItemTempate.



Lets say I have a template with id 123456, I want to get all items that are using this template. How would I do that?










share|improve this question






















  • How would you do that in C#?
    – Alan Płócieniak
    Nov 26 at 15:25










  • Something like allItems.Where(p => p.TemplateIdList.Contains("123456")); Let me see if I can powershell that! - Disclaimer - New to Sitecore so I don't know what exists and what doesn't...
    – Rick
    Nov 26 at 15:33
















4












4








4


1





The title pretty much says it all - I've looked at the docs but I just keep finding Get-Item and Get-ItemTempate.



Lets say I have a template with id 123456, I want to get all items that are using this template. How would I do that?










share|improve this question













The title pretty much says it all - I've looked at the docs but I just keep finding Get-Item and Get-ItemTempate.



Lets say I have a template with id 123456, I want to get all items that are using this template. How would I do that?







powershell-extensions






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 at 15:23









Rick

1505




1505












  • How would you do that in C#?
    – Alan Płócieniak
    Nov 26 at 15:25










  • Something like allItems.Where(p => p.TemplateIdList.Contains("123456")); Let me see if I can powershell that! - Disclaimer - New to Sitecore so I don't know what exists and what doesn't...
    – Rick
    Nov 26 at 15:33




















  • How would you do that in C#?
    – Alan Płócieniak
    Nov 26 at 15:25










  • Something like allItems.Where(p => p.TemplateIdList.Contains("123456")); Let me see if I can powershell that! - Disclaimer - New to Sitecore so I don't know what exists and what doesn't...
    – Rick
    Nov 26 at 15:33


















How would you do that in C#?
– Alan Płócieniak
Nov 26 at 15:25




How would you do that in C#?
– Alan Płócieniak
Nov 26 at 15:25












Something like allItems.Where(p => p.TemplateIdList.Contains("123456")); Let me see if I can powershell that! - Disclaimer - New to Sitecore so I don't know what exists and what doesn't...
– Rick
Nov 26 at 15:33






Something like allItems.Where(p => p.TemplateIdList.Contains("123456")); Let me see if I can powershell that! - Disclaimer - New to Sitecore so I don't know what exists and what doesn't...
– Rick
Nov 26 at 15:33












2 Answers
2






active

oldest

votes


















6














This should do it for you.



$defaultPath = "/sitecore/content"
[Sitecore.Data.ID]$articleId = "{03360FC1-B4C0-4770-9E1D-79E8317B74DD}"

$articles = Find-Item -Index sitecore_master_index `
-Where 'TemplateId = @0 and Path.StartsWith(@1)' `
-WhereValues $articleId, $defaultPath | Initialize-Item


Another way using template name and Criteria.



$articles = Find-Item `
-Index sitecore_master_index `
-Criteria @{Filter = "Equals"; Field = "_templatename"; Value = "Article"},
@{Filter = "Equals"; Field = "_language"; Value = "en"},
@{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content" }





share|improve this answer





















  • If I do a Write-Host $_.FullPath on everything in $articles, then I get quite a log of duplicates. Any idea why this would happen?
    – Rick
    Nov 26 at 16:52










  • (using the first example btw)
    – Rick
    Nov 26 at 17:08










  • multiple languages? This is is reading directly from the index, so we have to know why there are multiple items in your index.
    – Chris Auer
    Nov 26 at 17:18






  • 1




    Ah potentially - I'll investigate if that is the case. Thanks
    – Rick
    Nov 26 at 17:18



















3














There is a report included with SPE called Items with Template that can provide you with this information. This report scans a tree and checks for inheritance at any level. Slow because it has to look at every item. Incomplete because it only looks at the tree specified.



Sample Report Dialog



I would recommend you use the Get-ItemReferrer command. This is based on the Link Database and should be extremely fast; items that in no way are related are never requested. Be sure the Link Db is up-to-date before running.



A similar question was asked here.



Find related items



Example: The following returns all items referring to a given template.



$sampleItemTemplateId = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
$sampleItemTemplateItem = Get-Item -Path "master:" -ID $sampleItemTemplateId

Get-ItemReferrer -Item $sampleItemTemplateItem


As you can see below, all referrers are returned. You can then filter out with Where-Object if you want to exclude content, media, templates, etc.



enter image description here



Find related items at any level of inheritance



Example: The following returns all items referring to a given template that inherit that template. Demonstrates the use of the trampoline technique to reduce call depth. There are checks to ensure that the item exists and not duplicates are returned.



function Get-ItemBasedOnTemplate {
param(
[string]$TemplateId
)

$queue = New-Object System.Collections.Queue
$processedLookup = New-Object System.Collections.Generic.HashSet[string]
$foundItemIds = New-Object System.Collections.Generic.HashSet[string]
if(-not(Test-Path -Path "master:$($TemplateId)")) { return }
$processedLookup.Add($TemplateId) > $null
Get-ItemReferrer -Id $TemplateId -ItemLink |
Where-Object { $_.SourceItemID } |
ForEach-Object { $queue.Enqueue($_.SourceItemID) }

$database = Get-Database -Name "master"
while($queue.Count -and ($referrerId = $queue.Dequeue())) {
if($processedLookup.Contains($referrerId)) { continue }
$processedLookup.Add($referrerId) > $null
$referrer = $database.GetItem($referrerId)
if(!$referrer) { continue }
if(!$referrer.Paths.FullPath.StartsWith("/sitecore/templates")) {
$itemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($referrer)

if ($itemTemplate.DescendsFromOrEquals($TemplateId)) {
if($foundItemIds.Contains($referrer.ID)) { continue }
$foundItemIds.Add($referrer.ID) > $null
$referrer
}
} else {
if($referrer.Name -eq "__Standard values") { continue }
foreach($referrerItemLink in Get-ItemReferrer -Id $referrer.Id -ItemLink | Where-Object { $_.SourceItemID }) {
$queue.Enqueue($referrerItemLink.SourceItemID)
}
}
}
}


$baseTemplateId = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
Get-ItemBasedOnTemplate -TemplateId $baseTemplateId


Demo



The following demonstrates three base templates:
Base Template -> Base Template 1 -> Base Template 2



Query base template



Interesting discovery. Switched from Get-Item to $db.GetItem() and the results are considerably faster; SPE wraps objects returned Get-Item and Get-ChildItem with additional properties.



$baseTemplateId = "{3F8A6A5D-7B1A-4566-8CD4-0A50F3030BD8}"
$watch = [System.Diagnostics.Stopwatch]::StartNew()
$itemCount = Get-ItemBasedOnTemplate -TemplateId $baseTemplateId |
Measure-Object | Select-Object -Expand Count
$watch.Stop()
$time = $watch.ElapsedMilliseconds / 1000
Write-Host "Discovered and returned $($itemCount) items in $($time) seconds"

# Discovered and returned 1307 items in 0.527 seconds





share|improve this answer























  • Nice one Mike. Didn't know that existed.
    – Chris Auer
    Nov 26 at 17:12











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "664"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsitecore.stackexchange.com%2fquestions%2f15165%2fpowershell-get-all-items-that-use-a-template%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














This should do it for you.



$defaultPath = "/sitecore/content"
[Sitecore.Data.ID]$articleId = "{03360FC1-B4C0-4770-9E1D-79E8317B74DD}"

$articles = Find-Item -Index sitecore_master_index `
-Where 'TemplateId = @0 and Path.StartsWith(@1)' `
-WhereValues $articleId, $defaultPath | Initialize-Item


Another way using template name and Criteria.



$articles = Find-Item `
-Index sitecore_master_index `
-Criteria @{Filter = "Equals"; Field = "_templatename"; Value = "Article"},
@{Filter = "Equals"; Field = "_language"; Value = "en"},
@{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content" }





share|improve this answer





















  • If I do a Write-Host $_.FullPath on everything in $articles, then I get quite a log of duplicates. Any idea why this would happen?
    – Rick
    Nov 26 at 16:52










  • (using the first example btw)
    – Rick
    Nov 26 at 17:08










  • multiple languages? This is is reading directly from the index, so we have to know why there are multiple items in your index.
    – Chris Auer
    Nov 26 at 17:18






  • 1




    Ah potentially - I'll investigate if that is the case. Thanks
    – Rick
    Nov 26 at 17:18
















6














This should do it for you.



$defaultPath = "/sitecore/content"
[Sitecore.Data.ID]$articleId = "{03360FC1-B4C0-4770-9E1D-79E8317B74DD}"

$articles = Find-Item -Index sitecore_master_index `
-Where 'TemplateId = @0 and Path.StartsWith(@1)' `
-WhereValues $articleId, $defaultPath | Initialize-Item


Another way using template name and Criteria.



$articles = Find-Item `
-Index sitecore_master_index `
-Criteria @{Filter = "Equals"; Field = "_templatename"; Value = "Article"},
@{Filter = "Equals"; Field = "_language"; Value = "en"},
@{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content" }





share|improve this answer





















  • If I do a Write-Host $_.FullPath on everything in $articles, then I get quite a log of duplicates. Any idea why this would happen?
    – Rick
    Nov 26 at 16:52










  • (using the first example btw)
    – Rick
    Nov 26 at 17:08










  • multiple languages? This is is reading directly from the index, so we have to know why there are multiple items in your index.
    – Chris Auer
    Nov 26 at 17:18






  • 1




    Ah potentially - I'll investigate if that is the case. Thanks
    – Rick
    Nov 26 at 17:18














6












6








6






This should do it for you.



$defaultPath = "/sitecore/content"
[Sitecore.Data.ID]$articleId = "{03360FC1-B4C0-4770-9E1D-79E8317B74DD}"

$articles = Find-Item -Index sitecore_master_index `
-Where 'TemplateId = @0 and Path.StartsWith(@1)' `
-WhereValues $articleId, $defaultPath | Initialize-Item


Another way using template name and Criteria.



$articles = Find-Item `
-Index sitecore_master_index `
-Criteria @{Filter = "Equals"; Field = "_templatename"; Value = "Article"},
@{Filter = "Equals"; Field = "_language"; Value = "en"},
@{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content" }





share|improve this answer












This should do it for you.



$defaultPath = "/sitecore/content"
[Sitecore.Data.ID]$articleId = "{03360FC1-B4C0-4770-9E1D-79E8317B74DD}"

$articles = Find-Item -Index sitecore_master_index `
-Where 'TemplateId = @0 and Path.StartsWith(@1)' `
-WhereValues $articleId, $defaultPath | Initialize-Item


Another way using template name and Criteria.



$articles = Find-Item `
-Index sitecore_master_index `
-Criteria @{Filter = "Equals"; Field = "_templatename"; Value = "Article"},
@{Filter = "Equals"; Field = "_language"; Value = "en"},
@{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content" }






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 at 15:37









Chris Auer

7,13711142




7,13711142












  • If I do a Write-Host $_.FullPath on everything in $articles, then I get quite a log of duplicates. Any idea why this would happen?
    – Rick
    Nov 26 at 16:52










  • (using the first example btw)
    – Rick
    Nov 26 at 17:08










  • multiple languages? This is is reading directly from the index, so we have to know why there are multiple items in your index.
    – Chris Auer
    Nov 26 at 17:18






  • 1




    Ah potentially - I'll investigate if that is the case. Thanks
    – Rick
    Nov 26 at 17:18


















  • If I do a Write-Host $_.FullPath on everything in $articles, then I get quite a log of duplicates. Any idea why this would happen?
    – Rick
    Nov 26 at 16:52










  • (using the first example btw)
    – Rick
    Nov 26 at 17:08










  • multiple languages? This is is reading directly from the index, so we have to know why there are multiple items in your index.
    – Chris Auer
    Nov 26 at 17:18






  • 1




    Ah potentially - I'll investigate if that is the case. Thanks
    – Rick
    Nov 26 at 17:18
















If I do a Write-Host $_.FullPath on everything in $articles, then I get quite a log of duplicates. Any idea why this would happen?
– Rick
Nov 26 at 16:52




If I do a Write-Host $_.FullPath on everything in $articles, then I get quite a log of duplicates. Any idea why this would happen?
– Rick
Nov 26 at 16:52












(using the first example btw)
– Rick
Nov 26 at 17:08




(using the first example btw)
– Rick
Nov 26 at 17:08












multiple languages? This is is reading directly from the index, so we have to know why there are multiple items in your index.
– Chris Auer
Nov 26 at 17:18




multiple languages? This is is reading directly from the index, so we have to know why there are multiple items in your index.
– Chris Auer
Nov 26 at 17:18




1




1




Ah potentially - I'll investigate if that is the case. Thanks
– Rick
Nov 26 at 17:18




Ah potentially - I'll investigate if that is the case. Thanks
– Rick
Nov 26 at 17:18











3














There is a report included with SPE called Items with Template that can provide you with this information. This report scans a tree and checks for inheritance at any level. Slow because it has to look at every item. Incomplete because it only looks at the tree specified.



Sample Report Dialog



I would recommend you use the Get-ItemReferrer command. This is based on the Link Database and should be extremely fast; items that in no way are related are never requested. Be sure the Link Db is up-to-date before running.



A similar question was asked here.



Find related items



Example: The following returns all items referring to a given template.



$sampleItemTemplateId = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
$sampleItemTemplateItem = Get-Item -Path "master:" -ID $sampleItemTemplateId

Get-ItemReferrer -Item $sampleItemTemplateItem


As you can see below, all referrers are returned. You can then filter out with Where-Object if you want to exclude content, media, templates, etc.



enter image description here



Find related items at any level of inheritance



Example: The following returns all items referring to a given template that inherit that template. Demonstrates the use of the trampoline technique to reduce call depth. There are checks to ensure that the item exists and not duplicates are returned.



function Get-ItemBasedOnTemplate {
param(
[string]$TemplateId
)

$queue = New-Object System.Collections.Queue
$processedLookup = New-Object System.Collections.Generic.HashSet[string]
$foundItemIds = New-Object System.Collections.Generic.HashSet[string]
if(-not(Test-Path -Path "master:$($TemplateId)")) { return }
$processedLookup.Add($TemplateId) > $null
Get-ItemReferrer -Id $TemplateId -ItemLink |
Where-Object { $_.SourceItemID } |
ForEach-Object { $queue.Enqueue($_.SourceItemID) }

$database = Get-Database -Name "master"
while($queue.Count -and ($referrerId = $queue.Dequeue())) {
if($processedLookup.Contains($referrerId)) { continue }
$processedLookup.Add($referrerId) > $null
$referrer = $database.GetItem($referrerId)
if(!$referrer) { continue }
if(!$referrer.Paths.FullPath.StartsWith("/sitecore/templates")) {
$itemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($referrer)

if ($itemTemplate.DescendsFromOrEquals($TemplateId)) {
if($foundItemIds.Contains($referrer.ID)) { continue }
$foundItemIds.Add($referrer.ID) > $null
$referrer
}
} else {
if($referrer.Name -eq "__Standard values") { continue }
foreach($referrerItemLink in Get-ItemReferrer -Id $referrer.Id -ItemLink | Where-Object { $_.SourceItemID }) {
$queue.Enqueue($referrerItemLink.SourceItemID)
}
}
}
}


$baseTemplateId = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
Get-ItemBasedOnTemplate -TemplateId $baseTemplateId


Demo



The following demonstrates three base templates:
Base Template -> Base Template 1 -> Base Template 2



Query base template



Interesting discovery. Switched from Get-Item to $db.GetItem() and the results are considerably faster; SPE wraps objects returned Get-Item and Get-ChildItem with additional properties.



$baseTemplateId = "{3F8A6A5D-7B1A-4566-8CD4-0A50F3030BD8}"
$watch = [System.Diagnostics.Stopwatch]::StartNew()
$itemCount = Get-ItemBasedOnTemplate -TemplateId $baseTemplateId |
Measure-Object | Select-Object -Expand Count
$watch.Stop()
$time = $watch.ElapsedMilliseconds / 1000
Write-Host "Discovered and returned $($itemCount) items in $($time) seconds"

# Discovered and returned 1307 items in 0.527 seconds





share|improve this answer























  • Nice one Mike. Didn't know that existed.
    – Chris Auer
    Nov 26 at 17:12
















3














There is a report included with SPE called Items with Template that can provide you with this information. This report scans a tree and checks for inheritance at any level. Slow because it has to look at every item. Incomplete because it only looks at the tree specified.



Sample Report Dialog



I would recommend you use the Get-ItemReferrer command. This is based on the Link Database and should be extremely fast; items that in no way are related are never requested. Be sure the Link Db is up-to-date before running.



A similar question was asked here.



Find related items



Example: The following returns all items referring to a given template.



$sampleItemTemplateId = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
$sampleItemTemplateItem = Get-Item -Path "master:" -ID $sampleItemTemplateId

Get-ItemReferrer -Item $sampleItemTemplateItem


As you can see below, all referrers are returned. You can then filter out with Where-Object if you want to exclude content, media, templates, etc.



enter image description here



Find related items at any level of inheritance



Example: The following returns all items referring to a given template that inherit that template. Demonstrates the use of the trampoline technique to reduce call depth. There are checks to ensure that the item exists and not duplicates are returned.



function Get-ItemBasedOnTemplate {
param(
[string]$TemplateId
)

$queue = New-Object System.Collections.Queue
$processedLookup = New-Object System.Collections.Generic.HashSet[string]
$foundItemIds = New-Object System.Collections.Generic.HashSet[string]
if(-not(Test-Path -Path "master:$($TemplateId)")) { return }
$processedLookup.Add($TemplateId) > $null
Get-ItemReferrer -Id $TemplateId -ItemLink |
Where-Object { $_.SourceItemID } |
ForEach-Object { $queue.Enqueue($_.SourceItemID) }

$database = Get-Database -Name "master"
while($queue.Count -and ($referrerId = $queue.Dequeue())) {
if($processedLookup.Contains($referrerId)) { continue }
$processedLookup.Add($referrerId) > $null
$referrer = $database.GetItem($referrerId)
if(!$referrer) { continue }
if(!$referrer.Paths.FullPath.StartsWith("/sitecore/templates")) {
$itemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($referrer)

if ($itemTemplate.DescendsFromOrEquals($TemplateId)) {
if($foundItemIds.Contains($referrer.ID)) { continue }
$foundItemIds.Add($referrer.ID) > $null
$referrer
}
} else {
if($referrer.Name -eq "__Standard values") { continue }
foreach($referrerItemLink in Get-ItemReferrer -Id $referrer.Id -ItemLink | Where-Object { $_.SourceItemID }) {
$queue.Enqueue($referrerItemLink.SourceItemID)
}
}
}
}


$baseTemplateId = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
Get-ItemBasedOnTemplate -TemplateId $baseTemplateId


Demo



The following demonstrates three base templates:
Base Template -> Base Template 1 -> Base Template 2



Query base template



Interesting discovery. Switched from Get-Item to $db.GetItem() and the results are considerably faster; SPE wraps objects returned Get-Item and Get-ChildItem with additional properties.



$baseTemplateId = "{3F8A6A5D-7B1A-4566-8CD4-0A50F3030BD8}"
$watch = [System.Diagnostics.Stopwatch]::StartNew()
$itemCount = Get-ItemBasedOnTemplate -TemplateId $baseTemplateId |
Measure-Object | Select-Object -Expand Count
$watch.Stop()
$time = $watch.ElapsedMilliseconds / 1000
Write-Host "Discovered and returned $($itemCount) items in $($time) seconds"

# Discovered and returned 1307 items in 0.527 seconds





share|improve this answer























  • Nice one Mike. Didn't know that existed.
    – Chris Auer
    Nov 26 at 17:12














3












3








3






There is a report included with SPE called Items with Template that can provide you with this information. This report scans a tree and checks for inheritance at any level. Slow because it has to look at every item. Incomplete because it only looks at the tree specified.



Sample Report Dialog



I would recommend you use the Get-ItemReferrer command. This is based on the Link Database and should be extremely fast; items that in no way are related are never requested. Be sure the Link Db is up-to-date before running.



A similar question was asked here.



Find related items



Example: The following returns all items referring to a given template.



$sampleItemTemplateId = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
$sampleItemTemplateItem = Get-Item -Path "master:" -ID $sampleItemTemplateId

Get-ItemReferrer -Item $sampleItemTemplateItem


As you can see below, all referrers are returned. You can then filter out with Where-Object if you want to exclude content, media, templates, etc.



enter image description here



Find related items at any level of inheritance



Example: The following returns all items referring to a given template that inherit that template. Demonstrates the use of the trampoline technique to reduce call depth. There are checks to ensure that the item exists and not duplicates are returned.



function Get-ItemBasedOnTemplate {
param(
[string]$TemplateId
)

$queue = New-Object System.Collections.Queue
$processedLookup = New-Object System.Collections.Generic.HashSet[string]
$foundItemIds = New-Object System.Collections.Generic.HashSet[string]
if(-not(Test-Path -Path "master:$($TemplateId)")) { return }
$processedLookup.Add($TemplateId) > $null
Get-ItemReferrer -Id $TemplateId -ItemLink |
Where-Object { $_.SourceItemID } |
ForEach-Object { $queue.Enqueue($_.SourceItemID) }

$database = Get-Database -Name "master"
while($queue.Count -and ($referrerId = $queue.Dequeue())) {
if($processedLookup.Contains($referrerId)) { continue }
$processedLookup.Add($referrerId) > $null
$referrer = $database.GetItem($referrerId)
if(!$referrer) { continue }
if(!$referrer.Paths.FullPath.StartsWith("/sitecore/templates")) {
$itemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($referrer)

if ($itemTemplate.DescendsFromOrEquals($TemplateId)) {
if($foundItemIds.Contains($referrer.ID)) { continue }
$foundItemIds.Add($referrer.ID) > $null
$referrer
}
} else {
if($referrer.Name -eq "__Standard values") { continue }
foreach($referrerItemLink in Get-ItemReferrer -Id $referrer.Id -ItemLink | Where-Object { $_.SourceItemID }) {
$queue.Enqueue($referrerItemLink.SourceItemID)
}
}
}
}


$baseTemplateId = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
Get-ItemBasedOnTemplate -TemplateId $baseTemplateId


Demo



The following demonstrates three base templates:
Base Template -> Base Template 1 -> Base Template 2



Query base template



Interesting discovery. Switched from Get-Item to $db.GetItem() and the results are considerably faster; SPE wraps objects returned Get-Item and Get-ChildItem with additional properties.



$baseTemplateId = "{3F8A6A5D-7B1A-4566-8CD4-0A50F3030BD8}"
$watch = [System.Diagnostics.Stopwatch]::StartNew()
$itemCount = Get-ItemBasedOnTemplate -TemplateId $baseTemplateId |
Measure-Object | Select-Object -Expand Count
$watch.Stop()
$time = $watch.ElapsedMilliseconds / 1000
Write-Host "Discovered and returned $($itemCount) items in $($time) seconds"

# Discovered and returned 1307 items in 0.527 seconds





share|improve this answer














There is a report included with SPE called Items with Template that can provide you with this information. This report scans a tree and checks for inheritance at any level. Slow because it has to look at every item. Incomplete because it only looks at the tree specified.



Sample Report Dialog



I would recommend you use the Get-ItemReferrer command. This is based on the Link Database and should be extremely fast; items that in no way are related are never requested. Be sure the Link Db is up-to-date before running.



A similar question was asked here.



Find related items



Example: The following returns all items referring to a given template.



$sampleItemTemplateId = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
$sampleItemTemplateItem = Get-Item -Path "master:" -ID $sampleItemTemplateId

Get-ItemReferrer -Item $sampleItemTemplateItem


As you can see below, all referrers are returned. You can then filter out with Where-Object if you want to exclude content, media, templates, etc.



enter image description here



Find related items at any level of inheritance



Example: The following returns all items referring to a given template that inherit that template. Demonstrates the use of the trampoline technique to reduce call depth. There are checks to ensure that the item exists and not duplicates are returned.



function Get-ItemBasedOnTemplate {
param(
[string]$TemplateId
)

$queue = New-Object System.Collections.Queue
$processedLookup = New-Object System.Collections.Generic.HashSet[string]
$foundItemIds = New-Object System.Collections.Generic.HashSet[string]
if(-not(Test-Path -Path "master:$($TemplateId)")) { return }
$processedLookup.Add($TemplateId) > $null
Get-ItemReferrer -Id $TemplateId -ItemLink |
Where-Object { $_.SourceItemID } |
ForEach-Object { $queue.Enqueue($_.SourceItemID) }

$database = Get-Database -Name "master"
while($queue.Count -and ($referrerId = $queue.Dequeue())) {
if($processedLookup.Contains($referrerId)) { continue }
$processedLookup.Add($referrerId) > $null
$referrer = $database.GetItem($referrerId)
if(!$referrer) { continue }
if(!$referrer.Paths.FullPath.StartsWith("/sitecore/templates")) {
$itemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($referrer)

if ($itemTemplate.DescendsFromOrEquals($TemplateId)) {
if($foundItemIds.Contains($referrer.ID)) { continue }
$foundItemIds.Add($referrer.ID) > $null
$referrer
}
} else {
if($referrer.Name -eq "__Standard values") { continue }
foreach($referrerItemLink in Get-ItemReferrer -Id $referrer.Id -ItemLink | Where-Object { $_.SourceItemID }) {
$queue.Enqueue($referrerItemLink.SourceItemID)
}
}
}
}


$baseTemplateId = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
Get-ItemBasedOnTemplate -TemplateId $baseTemplateId


Demo



The following demonstrates three base templates:
Base Template -> Base Template 1 -> Base Template 2



Query base template



Interesting discovery. Switched from Get-Item to $db.GetItem() and the results are considerably faster; SPE wraps objects returned Get-Item and Get-ChildItem with additional properties.



$baseTemplateId = "{3F8A6A5D-7B1A-4566-8CD4-0A50F3030BD8}"
$watch = [System.Diagnostics.Stopwatch]::StartNew()
$itemCount = Get-ItemBasedOnTemplate -TemplateId $baseTemplateId |
Measure-Object | Select-Object -Expand Count
$watch.Stop()
$time = $watch.ElapsedMilliseconds / 1000
Write-Host "Discovered and returned $($itemCount) items in $($time) seconds"

# Discovered and returned 1307 items in 0.527 seconds






share|improve this answer














share|improve this answer



share|improve this answer








edited 8 hours ago

























answered Nov 26 at 16:03









Michael West

8,19621450




8,19621450












  • Nice one Mike. Didn't know that existed.
    – Chris Auer
    Nov 26 at 17:12


















  • Nice one Mike. Didn't know that existed.
    – Chris Auer
    Nov 26 at 17:12
















Nice one Mike. Didn't know that existed.
– Chris Auer
Nov 26 at 17:12




Nice one Mike. Didn't know that existed.
– Chris Auer
Nov 26 at 17:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Sitecore Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsitecore.stackexchange.com%2fquestions%2f15165%2fpowershell-get-all-items-that-use-a-template%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Probability when a professor distributes a quiz and homework assignment to a class of n students.

Aardman Animations

Are they similar matrix