8

I'm trying to list items which have a set template on the parent page in Sitecore. So far I can do it for the children but I also want to include the children's children, i.e. anything under the parent if it has the chosen template it will work, this is my code in the c# file:

lvThing.DataSource = context.Children.Where(x => x.TemplateName == "cool    template").ToList<Item>();
lvThing.DataBind();

1 Answer 1

14

If you want the items below the children, you can use the item.Axes.GetDescendants() method to get all items below the context item.

Your code then should look like this:

contextItem.Axes.GetDescendants().Where(x => x.TemplateName == "cool    template").ToList();
4
  • Hi, thanks for reply, but getting an error, using: lvThing.DataSource = contextItem.Axes.GetDescendants().Where(x => x.TemplateName == "cool template").ToList<Item>();
    – David
    Nov 22, 2012 at 15:25
  • ah, got to to work by changing contextitem to context. Thanks for the help :-)
    – David
    Nov 22, 2012 at 15:31
  • I'd be careful using GetDescendants it can be pretty expensive.
    – Anicho
    Mar 7, 2014 at 17:04
  • 3
    Given the additional filtering applied, you might consider contextItem.Axes.SelectItems(string query), which lets you use a sitecore query executed relative to the contextItem. (ex: contextItem.Axes.SelectItems("descendant-or-self::*[@@templatename='cool template']") Jun 17, 2014 at 12:19

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.