Get Items Of Type

Get Items Of Type

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Items.Item
  • Sitecore.Links.LinkManager
  • System.Linq

Items Of Type C# Web Control Code

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

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

var sortedList = (from entry in descendants orderby entry.Fields["title"].Value ascending select entry);

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}", "Parent Item", item.DisplayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", " First Ten Code Snippet Pages");
output.RenderEndTag(); //P

int i = 0;
foreach (Sitecore.Data.Items.Item descendant in sortedList)
{
    if (descendant.TemplateName == "TIC-CodeSnippet")
    {
        i++;
        output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, Sitecore.Links.LinkManager.GetItemUrl(descendant));
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
        if(descendant.Fields["Title"] != null)
output.Write("{0}", descendant.Fields["Title"] + "<br/>");
        output.RenderEndTag();  //A
    }
    //only display first 10
    if (i >= 10) break;
}

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", " First Ten Snippet Type Pages");
output.RenderEndTag(); //P

int j = 0;
foreach (Sitecore.Data.Items.Item descendant in sortedList)
{
    Sitecore.Data.Items.TemplateItem[] templates = descendant.Template.BaseTemplates;
    if (templates != null)
    {
        foreach (Sitecore.Data.Items.TemplateItem baseTemplate in templates)
        {
if (baseTemplate.Name.Equals("TIC-SnipTypePage"))
{
    j++;
    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, Sitecore.Links.LinkManager.GetItemUrl(descendant));
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
    if (descendant.Fields["Title"] != null)
        output.Write("{0}", descendant.Fields["Title"] + "<br/>");
    output.RenderEndTag();  //A
}
        }
    }
    //only display first 10
    if (j >= 10) break;
}

output.RenderEndTag(); //Div


Items Of Type 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="'Parent Item'"/>
&#58;&#160;<sc:text field="title" select="$item"/></b></p>

<p><xsl:value-of select="'First Ten Code Snippet Pages'"/></p>

<xsl:for-each select="$item//item[@template='tic-codesnippet']">
      <xsl:sort select="sc:fld('title',.)" data-type="text" order="ascending"/>
      <xsl:if test="position()&lt; 11"> 
          <sc:link select="." title="{sc:fld('title',.)}" disable-web-editing="true">
              <sc:text field="title" select="." disable-web-editing="true"/>
            </sc:link><br/>
      </xsl:if>
</xsl:for-each>

<p><xsl:value-of select="'First Ten Snippet Type Pages'"/></p>
<xsl:for-each select="sc:GetItemsOfType('tic-sniptypepage',$item//item)">
    <xsl:sort select="sc:fld('title',.)" data-type="text" order="ascending"/>
    <xsl:if test="position()&lt; 11">  
        <sc:link select="." title="{sc:fld('title',.)}" disable-web-editing="true">
              <sc:text field="title" select="." disable-web-editing="true"/>
        </sc:link><br/>
    </xsl:if>
</xsl:for-each>

</div>


Items Of Type C# Web Control Output


Notes

  • TIC-CodeSnippet is the template used to create pages that contain code examples
  • TIC-ConfigSetting is the template used to create pages that contain configuration settings, sometimes with code examples
  • Both of these templates inherit from the TIC-SnipTypePage template
  • The first output list only contains code example pages
  • The second output list contains code examples and configuration settings pages