I want to find the ancestor of an item that matches the given template id using Powershell. Can anyone help me in achieving this?

Scenario: I'm on Item A, I get the current item details using gi .. From item A I want to navigate upwards and find the ancestor with matching TemplateID.

Example:

Home

-- Parent 1 

  -- Parent 2

     -- Item A
  • Can you provide more details with an example if possible – TamerM Oct 31 at 14:15

If I understand your requirement, you want to find all items related to a known template ID. The phrasing is a bit confusing so it would be helpful if you can clarify the statement.

Example: Given an item, find all items matching the same Template ID which are children of the given item parent. In other words, find all items with the same template as Home which fall under the Content node (parent of Home).

$item = Get-Item -Path "master:\content\home"
$templateItem = Get-ItemTemplate -Item $item
Get-ItemReferrer -Item $templateItem | 
    Where-Object { $_.ItemPath.StartsWith($item.Parent.Paths.Path) }

If I understood right, which is 'find the ancestor that matches the current item's template' (the opposite of Michael's answer :-) ), you could use the below script:

$currentItem = Get-Item -Path '/sitecore/content/Home/'
$templateId = "" #Place your target template ID here
$currentItem.Name

$item = $currentItem
while($item.Parent){
    $item.Parent.Name
    if($item.Parent.TemplateId -eq $templateId){
        "done. item.Parent is what you're looking for"
        $item.Parent.Id #target item's ID
        break
    }
    $item = $item.Parent
}

You will have to set the $currentItem and $templateId to match your scenario

  • no @TamerM, the templateid of the parent is different. actually in a tree of items, my item is in the end, i want to navigate and find the parent's belongs to different templates and get their Item ID. – Prakash Oct 31 at 15:09
  • 1
    @TamerM, if you can, just update your answer so the $templateId is hardcoded to something. @Prakash just wants to know when sitting at a random item, walk up the tree until you find a known template ID. – Michael West Oct 31 at 15:28
  • @Prakash I updated the answer by making the template ID static. You will have to put in your template ID to search for – TamerM Oct 31 at 15:57
  • @TamerM thanks for the help and found an alternate way in getting the templateID details $currentItemId = gi . #get current item $currentItemPath = $currentItemId.ItemPath #retrieve item path $splitedArray = $path -split '/' #perform split operation $pathBuilder = "" foreach($result in $splitedArray) { $pathBuilder = ($pathBuilder)+'/'+$result $itemDetails = gi -Path master:/$pathBuilder $templateId = $itemDetails.TemplateId if($templateId -eq "")#pass the desired templateId here { } } – Prakash Nov 1 at 7:18

Finally found the way using the Item path and traversed the tree and found the template Id

$currentItemId = gi . #get current item 
$currentItemPath = $currentItemId.ItemPath #retrieve item path
$splitedArray = $path -split '/' #perform split operation 
$pathBuilder = ""
foreach($result in $splitedArray)
{

$pathBuilder = ($pathBuilder)+'/'+$result
$itemDetails = gi -Path master:/$pathBuilder
$templateId = $itemDetails.TemplateId
if($templateId -eq "")#pass the desired templateId here
{
 #..
}
}

Thanks a ton for @TamerM and @Michaell West

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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