Counting Pages

Counting Pages

Controls featured in this example:

  • Sitecore Web Control
  • XSLT Rendering

Classes featured in this example:

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

Counting Pages C# Web Control Code

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

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");
if (home == null) return;

Sitecore.Collections.ChildList children = home.GetChildren();
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);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Page Count by Site Section");
output.RenderEndTag(); //P

foreach (Sitecore.Data.Items.Item child in children)
{
string totaItems = child.Axes.GetDescendants().GetLength(0).ToString();
output.Write("{0}&#58;&#160;{1}", child.Fields["Menu Title"], "<b>" + totaItems + " pages</b><br/>");
}

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Code Pages by Site Section");
output.RenderEndTag(); //P

foreach (Sitecore.Data.Items.Item child in children)
{
Sitecore.Data.Items.Item[] descendants = child.Axes.GetDescendants();
if (descendants == null) return;
int snipTotal = 0;
foreach( Sitecore.Data.Items.Item descendant in descendants)
{
if (descendant.TemplateName == "TIC-CodeSnippet")
{
snipTotal++;
}
}
string codeSnippetTotal = snipTotal.ToString();
output.Write("{0}&#58;&#160;{1}", child.Fields["Menu Title"], "<b>" + codeSnippetTotal + " pages</b><br/>");
}

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Configuration Settings Pages by Site Section");
output.RenderEndTag(); //P

foreach (Sitecore.Data.Items.Item child in children)
{
Sitecore.Data.Items.Item[] descendants = child.Axes.GetDescendants();
if (descendants == null) return;
int configTotal = 0;
foreach (Sitecore.Data.Items.Item descendant in descendants)
{
if (descendant.TemplateName == "TIC-ConfigSetting")
{
configTotal++;
}
}
string configTotalItems = configTotal.ToString();
output.Write("{0}&#58;&#160;{1}", child.Fields["Menu Title"], "<b>" + configTotalItems + " pages</b><br/>");
}

output.RenderEndTag(); //Div


Counting Pages XSLT Rendering Code

<xsl:variable name="home" select="sc:item('/sitecore/content/home',.)" />
<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">
<p><b><xsl:value-of select="'Item: '" /><sc:text field="title" select="$sc_item" /></b></p>
<p>Page Count by Site Section</p>
<xsl:for-each select="$home/item">
    <sc:text field="menu title"/>: <b><xsl:value-of select="count(descendant::item)" /> pages</b><br/> 
</xsl:for-each>

<p>Code Snippet Pages by Site Section</p>
<xsl:for-each select="$home/item">
 <sc:text field="menu title"/>: <b><xsl:value-of select="count(descendant::item[@template='tic-codesnippet'])" /> pages</b><br/>
</xsl:for-each>

<p>Configuration Settings Pages by Site Section</p>
<xsl:for-each select="$home/item">
<sc:text field="menu title"/>: <b><xsl:value-of select="count(descendant::item[@template='tic-configsetting'])" /> pages</b><br/> 
</xsl:for-each>
</div>


Counting Pages C# Web Control Output

Page Count by Site Section

Content Authors: 71 pages
Marketers: 107 pages
Web Designers: 39 pages
Project Leads: 81 pages
Site Admins: 162 pages
Developers: 605 pages
Site Directory: 59 pages
0 pages
0 pages
0 pages
0 pages

Code Pages by Site Section

Content Authors: 0 pages
Marketers: 0 pages
Web Designers: 0 pages
Project Leads: 0 pages
Site Admins: 0 pages
Developers: 320 pages
Site Directory: 0 pages
0 pages
0 pages
0 pages
0 pages

Configuration Settings Pages by Site Section

Content Authors: 0 pages
Marketers: 2 pages
Web Designers: 1 pages
Project Leads: 1 pages
Site Admins: 8 pages
Developers: 67 pages
Site Directory: 0 pages
0 pages
0 pages
0 pages
0 pages


Counting Pages XSLT Rendering Output

Page Count by Site Section

Content Authors: 71 pages
Marketers: 107 pages
Web Designers: 39 pages
Project Leads: 81 pages
Site Admins: 162 pages
Developers: 605 pages
Site Directory: 59 pages
0 pages
0 pages
0 pages
0 pages

Code Snippet Pages by Site Section

Content Authors: 0 pages
Marketers: 0 pages
Web Designers: 0 pages
Project Leads: 0 pages
Site Admins: 0 pages
Developers: 320 pages
Site Directory: 0 pages
0 pages
0 pages
0 pages
0 pages

Configuration Settings Pages by Site Section

Content Authors: 0 pages
Marketers: 2 pages
Web Designers: 1 pages
Project Leads: 1 pages
Site Admins: 8 pages
Developers: 67 pages
Site Directory: 0 pages
0 pages
0 pages
0 pages
0 pages