Getting children children in sitecore

David picture David · Nov 22, 2012 · Viewed 15.7k times · Source

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();

Answer

Martijn van der Put picture Martijn van der Put · Nov 22, 2012

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();