1

How to get the items with specific template only. I need this, beacuse it is useful for the bucketable items, like Tags

Here is my code:

$props = @{
    Title = "Test"
    Description = "Test"
    OkButtonName = "Run Report"
    CancelButtonName = "Cancel"
    Parameters = @(
        @{ Name = "TagName"; Title = "Tag Name"; Editor = "multilist"; Source = "/sitecore/system/Settings/Buckets/TagRepository/" }
        @{ Title = "Note"; Value = "SitecoreExchange"; Editor = "info" }
    )
}

Here is the path (where I want specific template):

Source = "/sitecore/system/Settings/Buckets/TagRepository/"

4 Answers 4

2

Sitecore PowerShell has a report to get items by a specific ID:

<#
    .SYNOPSIS
        Lists all content items that inherit from a given template
        
    .NOTES
        Alex Washtell
#>

$database = "master"
$root = Get-Item -Path (@{$true="$($database):\content\home"; $false="$($database):\content"}[(Test-Path -Path "$($database):\content\home")])
$baseTemplate = Get-Item master:\templates

$props = @{
    Parameters = @(
        @{Name="root"; Title="Choose the report root"; Tooltip="Only items from this root will be returned."; }
        @{ Name = "baseTemplate"; Title="Base Template"; Tooltip="Select the item to use as a base template for the report"; Root="/sitecore/templates/"}
    )
    Title = "Items With Template Report"
    Description = "Choose the criteria for the report."
    Width = 550
    Height = 300
    ShowHints = $true
    Icon = [regex]::Replace($PSScript.Appearance.Icon, "Office", "OfficeWhite", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
}

$result = Read-Variable @props

if($result -eq "cancel") {
    exit
}

filter Where-InheritsTemplate {
    param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [Sitecore.Data.Items.Item]$item
    )
    
    if ($item) {
        $itemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($item)

        if ($itemTemplate.DescendsFromOrEquals($baseTemplate.ID)) {
            $Item
        }
    }
}

$items = @($root) + @(($root.Axes.GetDescendants() | Initialize-Item)) | Where-InheritsTemplate

if($items.Count -eq 0) {
    Show-Alert "There are no content items that inherit from this template"
} else {
    $props = @{
        Title = "Item Template Report"
        InfoTitle = "Items that inherit from the '$($baseTemplate.Name)' template"
        InfoDescription = "The following items all inherit from the '$($baseTemplate.FullPath)' template."
        PageSize = 25
    }
    
    $items |
        Show-ListView @props -Property @{Label="Icon"; Expression={$_.__Icon} },
            @{Label="Name"; Expression={$_.DisplayName} },
            @{Label="Updated"; Expression={$_.__Updated} },
            @{Label="Updated by"; Expression={$_."__Updated by"} },
            @{Label="Created"; Expression={$_.__Created} },
            @{Label="Created by"; Expression={$_."__Created by"} },
            @{Label="Path"; Expression={$_.ItemPath} }
}

Close-Window
1
  • @VladLobagiu Thank you, but this does not answer my question.
    – Samke11
    Dec 16, 2021 at 19:50
1

You can get the items with a specific template only via template ID:

$allItem = Get-ChildItem -Path $itemPath -Recurse | where-object 
{$_.TemplateID -eq "{1D5C266A-112F-4EA2-A69E-E4865ACE2200}"}

Here in $itemPath you can assign the root path and it will return you all child items with specified TemplateId.

You can update condition like instead of TemplateId you want to search with TemplateName then update code:

$_.TemplateName -eq "TemplateName"

If you want to get items on the basis of Field value then:

$_.Fields["FieldName"].Value

You can use Find-Item command as well.

1
  • I know about this, thank you for answering. Still my question is how to get this in Source field lets say, in Parameters
    – Samke11
    Dec 17, 2021 at 9:25
0

I fixed the problem by just adding the /*/*/*/*/* at the end of the Source in Parameters Object.

$props = @{
    Title = "Get All Media Items by Tag"
    Description = "Get all Media items by Tag"
    OkButtonName = "Run Report"
    CancelButtonName = "Cancel"
    Parameters = @(  
        @{ Name = "TagName"; Title = "Tag Name"; Editor = "multilist"; Source = "/sitecore/system/Settings/Buckets/TagRepository/*/*/*/*/*"}
        @{ Title = "Note"; Value = "Select Tags you need for the Images from the Tag Repository"; Editor = "info" }
    )
}
0

You have to use the template query in your source like this : /sitecore/system/Settings/Buckets/TagRepository/[@@templatename='TagTemplateName']

and don’t forget to replace the TagTemplateName with the name of the template you want to filter.

working code

 $props = @{
    Title = "Test"
    Description = "Test"
    OkButtonName = "Run Report"
    CancelButtonName = "Cancel"
    Parameters = @(
        @{ Name = "TagName"; Title = "Tag Name"; Editor = "multilist"; Source = "/sitecore/system/Settings/Buckets/TagRepository/[@@templatename='TagTemplateName']" }
        @{ Title = "Note"; Value = "SitecoreExchange"; Editor = "info" }
    )
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.