1

It's easier to explain what I'm trying to do with an image, so let's go!

enter image description here

I need a query to get all parent items that have at least one Promotion Content as a child. The same for the contrary, I need to get all parent items that do not have promotion content as a child.

Here is how my query is currently.

/sitecore/content/Home/Articles//*[@@templatename = 'Article' and @__Never publish = '']

I have no idea how I can filter parent items based on their children's templates using Sitecore queries.

I know we can do this in many ways using PowerShell scripts but my intention here is to figure out whether it's possible to filter like this using only Sitecore queries.

3 Answers 3

1

In PowerShell in you can easily do that, e.g. I have created a function to count the child and then called it in query :

 function havechild($item) {
    $ct=(get-childitem $item ).count
 
    if ($ct -gt 0) {
          write-host $item.fullpath
        return $true
    }
    else
        {
          
           return $false
       }
}



get-childitem 'your path to search' -recurse | where {$_.templateid -eq 'template id whose child count need to find' -and 
(havechild $_.fullpath) -eq $true
    
}

It will give a list of all items having children of a template type.

Hope it helps.

1
  • I'll use your suggestion in the last case, however, my first intention here is to figure out if we can do this filter using a Sitecore query. Thank you for answering! Jun 7 at 9:52
1

There is something I found and hope this can help you. I checked that if there is a way in the query we can use count like if the count of the child items is > 0. But looks like we cannot use. So I found one that you can try like this.

../*[@@templatekey='Article' and *!=null]

or like below

../*[@@templatename='Article' and *!=null]

I also found a document that has lots of syntaxes that you can use to filter the records. It contains an example of the content tree structure at the top and query syntaxes to filter the records with the expected result.

https://doc.sitecore.com/xp/en/sdnarchive/reference/using%20sitecore%20query/sitecore%20query%20syntax.html

Hope this helps.

1

After taking a look at Sitecore query documentation I found that https://doc.sitecore.com/xp/en/SdnArchive/SDN5/Reference/Using%20Sitecore%20Query/Sitecore%20Query%20Syntax.html

enter image description here

So in the end my query was like that

/sitecore/content/Home/Articles//*[@@templatename = 'PromotionContent']/..

This is what I wanted, by doing this I can get all Items that have at least a PromotionContent as a child.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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