Get Descendants

Get Descendants

Descendant Ordering

  • Item.Axes.GetDescendants() is used to retrieve a collection of descendant items
  • XSLT orders descendants differently than ASP.NET
  • ASP.NET lists immediate child items first
  • XSLT orders all descendants under each child item before moving to next child item

Controls featured in this example:

  • Sitecore Web Control
  • XSLT Rendering

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.Item

Descendants C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item.Parent.Parent;
if (item == null) return;

Sitecore.Data.Items.Item[] children = item.Axes.GetDescendants();
if (children == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.Write("C# Web Control Real-time Output");

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.Write("{0}: {1}", "Parent Item", item.DisplayName);
output.RenderEndTag(); //P
output.Write("{0}", "Displaying First 10 Descendants");
output.RenderEndTag(); //P

int i = 0;
foreach (Sitecore.Data.Items.Item descendant in children)
    output.Write("{0}", descendant.DisplayName);
    output.RenderEndTag(); //Li

    //display only first ten descendants
    if(i >= 10)

output.RenderEndTag(); //Ol
output.RenderEndTag(); //Div

Descendants XSLT Rendering Code

<xsl:variable name="item" select="./../.." />
<div style="font-weight:bold; width:100%; clear:both"> <br/>
<xsl:value-of select="'XSLT Rendering Real-time Output'"/>

<div style="font-size:x-small; width:100%; clear:both">
<p><b>Parent Item: <sc:text field="title" select="$item" disable-web-editing="true"/></b></p>
<p><xsl:value-of select="'Displaying First 10 Descendants'"/></p>

<xsl:for-each select="$item/descendant::item">
<xsl:if test="position()&lt; 11">  
  <li><sc:text field="title"/></li>


Descendants C# Web Control Output

Parent Item: Content Display

Displaying First 10 Descendants

  1. Ancestor Axis
  2. Descendant Axis
  3. Field Values
  4. Icon Management
  5. Internationalization
  6. Item Attributes
  7. Item Locking
  8. Item Management
  9. Link Management
  10. Sitecore Queries

Descendants XSLT Rendering Output

Parent Item: Sitecore Content Display

Displaying First 10 Descendants

  1. Ancestor Axis
  2. Document Order
  3. Get Ancestors
  4. Get Home Item
  5. Get Parent Item
  6. Reverse Doc Order
  7. Using Recursion
  8. Descendant Axis
  9. ChildList Sort
  10. Children Property