Counting Items

Counting Items

Controls featured in this example:

  • Sitecore Web Control
  • XSLT Rendering

Classes featured in this example:

  • Sitecore.Data.Items.Item

Counting Items C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem().Parent.Parent;
if (item == 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);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "Child Item Count", "<b>" + item.Children.Count.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Descendant Count", "<b>" + item.Axes.GetDescendants().GetLength(0).ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Ancestor Count", "<b>" + item.Axes.GetAncestors().GetLength(0).ToString() + "</b><br/>");
int siblings = item.Parent.Children.Count - 1;
output.Write("{0}&#58;&#160;{1}", "Sibling Count", "<b>" + siblings.ToString() + "</b>");
output.RenderEndTag(); //Div


Counting Items XSLT Rendering Code

<xsl:variable name="item" select="../.." />
<br/>
<div style="font-weight:bold; width:100%; clear:both"> <br/>
<xsl:value-of select="'XSLT Rendering Real-time Output'"/>
</div>
<div style="font-size:x-small; width:100%; clear:both">
<!-- using &#58; for colon and &#160; for space -->
<p><b><xsl:value-of select="'Item'" />&#58;&#160;<sc:text field="title" select="$item" /></b></p>
<xsl:value-of select="'Child Item Count'"/>&#58;&#160;<b><xsl:value-of select="count($item/item)" /></b><br/>
<xsl:value-of select="'Descendant Count'"/>&#58;&#160;<b><xsl:value-of select="count($item/descendant::item)" /></b><br/>
<xsl:value-of select="'Ancestor Count'"/>&#58;&#160;<b><xsl:value-of select="count($item/ancestor::item)" /></b><br/>
<xsl:value-of select="'Sibling Count'"/>&#58;&#160;<b><xsl:value-of select="count($item/parent::item/item)-1" /></b><br/>
</div>

Counting Items C# Web Control Output

Child Item Count: 11
Descendant Count: 121
Ancestor Count: 4
Sibling Count: 15



Counting Items XSLT Rendering Output

Child Item Count: 11
Descendant Count: 121
Ancestor Count: 4
Sibling Count: 15