Children Property

Children Property

Sitecore.Data.Items.Children

  • Used to retrieve a collection of child items using Sitecore.Collections.ChildList class
  • Sitecore.Collections.ChildList is faster than item.Children property because it makes a function call

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

Children Property C# Web Control Code

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item designersItem = db.GetItem("/sitecore/content/home/webdesigners");

if (designersItem != null)
{
    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}", "Parent Item", designersItem.Name);
    output.RenderEndTag(); //P
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Ol);

    Sitecore.Collections.ChildList children = designersItem.Children;

    if (children != null)
    { 
        for (int i = 0; i < children.Count; i++)
        {
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
output.Write("{0}&#58;&#160;{1}", "Child Item", children[i].Name);
output.RenderEndTag(); //Li
        }
    }
       
    output.RenderEndTag(); //Ol
    output.RenderEndTag(); //Div


Children Property C# Web Control Output

Parent Item: WebDesigners

  1. Child Item: CSSStylesheets
  2. Child Item: HTMLSupport
  3. Child Item: PerformanceDesign