Filter Descendants

Filter Descendants

Controls featured in this example:

  • Sitecore Web Control
  • XSLT Rendering

Classes featured in this example:

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

Filter Descendants C# Web Control Code

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

Sitecore.Data.Items.Item[] descendants = item.Axes.GetDescendants();
if (descendants == 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);
//using : for colon and   for space
output.Write("{0}: {1}", "Ancestor Item", item.DisplayName);
output.RenderEndTag(); //P
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Ol);

foreach (Sitecore.Data.Items.Item descendant in descendants)
{
    if (descendant.TemplateName == "TIC-ConfigSetting")
    {
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
        output.Write("{0}&#58;&#160;{1}", "Filtered Descendant", "<b>" + descendant.DisplayName + "</b>");
        output.RenderEndTag(); //Li
    }  
}

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


Filter Descendants XSLT Rendering Code

<xsl:variable name="item" select="./../.." />
<xsl:variable name="filter" select="'tic-configsetting'" />
<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>Ancestor Item: <xsl:value-of select="$item/@name" /></b></p>
<ol>
<xsl:for-each select="$item/descendant::item">
   <xsl:if test="self::item[@template=$filter]">
        <li>Filtered Descendant: <b><xsl:value-of select="./@name"/></b></li>
    </xsl:if> 
</xsl:for-each>
</ol>
</div>


Filter Descendants C# Web Control Output

Ancestor Item: Content Display

  1. Filtered Descendant: Markup In Fields
  2. Filtered Descendant: Icon Settings
  3. Filtered Descendant: Item Settings
  4. Filtered Descendant: Valid Item Names
  5. Filtered Descendant: Lock Settings
  6. Filtered Descendant: Item Not Found
  7. Filtered Descendant: UrlOptions Defaults
  8. Filtered Descendant: Using LinkManager
  9. Filtered Descendant: Query Settings
  10. Filtered Descendant: Setting Max Returns

Filter Descendants XSLT Rendering Output

Ancestor Item: ContentDisplay

  1. Filtered Descendant: MarkupInFields
  2. Filtered Descendant: IconSettings
  3. Filtered Descendant: ItemSettings
  4. Filtered Descendant: ValidItemNames
  5. Filtered Descendant: LockSettings
  6. Filtered Descendant: ItemNotFound
  7. Filtered Descendant: UrlOptionsDefaults
  8. Filtered Descendant: UsingLinkManager
  9. Filtered Descendant: QuerySettings
  10. Filtered Descendant: SettingMaxReturns

Notes

  • This C# code is useful because Sitecore.Context.Item.getChildren() method and Sitecore.Context.Item.Children property both return just the top level children