Using ArrayList

Using ArrayList

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • System.Collections.ArrayList
  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • ArrayList C# Web Control Code

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

    System.Collections.ArrayList children = new System.Collections.ArrayList();
    if (children == null) return;

    Sitecore.Data.Items.Item[] descendants = item.Axes.GetDescendants();

    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

    int i = 0;
    foreach (Sitecore.Data.Items.Item descendant in descendants)
    {
    i++;
    children.Add(descendant);

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

    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Ol);
    foreach (Sitecore.Data.Items.Item child in children)
    {
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
    output.Write("{0}", child.DisplayName);
    output.RenderEndTag(); //Li

    }

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


    ArrayList 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. Sitecore Queries

    Notes

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