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.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

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

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

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

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>

<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>

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

</xsl:for-each>
</ol>
</div>


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