Using ItemList

Using ItemList

Sitecore.Collections.ItemList Class

  • Child items can be sorted
  • Child items can be filtered

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Collections.ItemList
  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • Sitecore.StringUtil

Using ItemList C# Web Control Code

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

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

Sitecore.Collections.ItemList items = new Sitecore.Collections.ItemList();
Sitecore.Data.Items.Item[] childItems = ancestor.Children.ToArray();
if (childItems == 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);
string displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", displayName);
output.RenderEndTag(); //P
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Ol);

foreach (Sitecore.Data.Items.Item child in childItems)
{
    if (child.Name.Contains("Items"))
    {
        items.Add(child);
    }

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


C# Web Control Real-time Output

Item: Using ItemList