Using ChildList

Using ChildList

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Collections.ChildList
  • Sitecore.Data.Items.Item

ChildList C# Web Control Code

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

Sitecore.Collections.ChildList children = item.GetChildren();
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

foreach (Sitecore.Data.Items.Item child in children)
{
    output.Write("{0}&#58;&#160;{1}", "Child Item", "<b>" + child.DisplayName + "</b><br/>");
}

output.RenderEndTag(); //Div


ChildList C# Web Control Output

Parent Item: Descendant Axis

Child Item: ChildList Sort
Child Item: Children Property
Child Item: Counting Pages
Child Item: Deleting Child Items
Child Item: Descending Sort
Child Item: DisplayName Sort
Child Item: Filter Descendants
Child Item: Filtering Values
Child Item: Get Child Items
Child Item: Get Descendants
Child Item: Get Items Of Type
Child Item: Linq Filter Sort
Child Item: Reset Renderings
Child Item: Using ArrayList
Child Item: Using ChildList
Child Item: Using For Loop
Child Item: Using ItemList
Child Item: Using Tables
Child Item: Using ToArray()

Notes

  • The Sitecore.Context.Item.getChildren() method and Sitecore.Context.Item.Children property both return just the top level children
  • Sitecore.Collections.ChildList is faster than item.Children property because it makes a function call