Using ToArray()

Using ToArray()

Controls featured in this example:

  • Sitecore Web Control
  • XSLT Rendering

Classes featured in this example:

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

Using ToArray() C# Web Control Code

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

Sitecore.Data.Items.Item[] children = item.Children.ToArray();
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
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Displaying First 10 Descendants");
output.RenderEndTag(); //P
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Ol);

int i = 0;
foreach (Sitecore.Data.Items.Item descendant in children)
{
i++;
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
output.Write("{0}", descendant.DisplayName);
output.RenderEndTag(); //Li

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

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


Using ToArray() 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. Link Management
  10. Sitecore Queries

Notes

  • Sitecore.Collections.ChildList is faster than item.Children property because it makes a function call