Enumerate Templates

Enumerate Templates

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Configuration.Factory
  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item
  • Sitecore.Data.Items.TemplateItem
  • Sitecore.Data.TemplateRecords
  • System.Linq
  • System.Text.StringBuilder

Enumerate Templates C# Web Control Code

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

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Globalization.Language lang = Sitecore.Context.Language;
System.Text.StringBuilder builder = new System.Text.StringBuilder();

Sitecore.Data.TemplateRecords templateRecords = db.Templates;
Sitecore.Data.Items.TemplateItem[] templateItems = templateRecords.GetTemplates(lang);

var sortedList = (from entry in templateItems orderby entry.Name ascending select entry);

int i = 0;
foreach (var templateItem in sortedList)
{
    i++;
    builder.Append("Template Name: <b>" + templateItem.Name + "</b><br/>");
    builder.Append("Template Display Name: <b>" + templateItem.DisplayName + "</b><br/>");
    builder.Append("Template Full Name: <b>" + templateItem.FullName + "</b><br/>");
    builder.Append("Template ID: <b>" + templateItem.ID + "</b><br/>");
    builder.Append("Template Icon: <b>" + templateItem.Icon + "</b><br/>");
    if(i < 10) 
        builder.Append ("<hr/>");
    else
        break;
}

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("{0}", "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}&#58;&#160;{1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Displaying first ten templates");
output.RenderEndTag(); //P

output.Write("{0}", builder.ToString());

output.RenderEndTag(); //Div


Enumerate Templates C# Web Control Output

Displaying first ten templates

 

 

Template Name: __Standard Values
Template Display Name: __Standard Values
Template Full Name: System/Templates/Template/__Standard Values
Template ID: {154DE801-C1E7-4F68-9F0A-26676C1CAAFB}
Template Icon: Applications/16x16/form_blue.png

 

 

Template Name: Action
Template Display Name: Action
Template Full Name: System/Rules/Action
Template ID: {F90052A5-B4E6-4E6D-9812-1E1B88A6FCEA}
Template Icon: Software/32x32/shape_rectangle.png

 

 

Template Name: Action
Template Display Name: Action
Template Full Name: System/Workflow/Action
Template ID: {66882E97-C8AA-4E37-8901-7A8AA35ED2ED}
Template Icon: Applications/16x16/gear_run.png

 

 

Template Name: Advanced
Template Display Name: Advanced
Template Full Name: System/Templates/Sections/Advanced
Template ID: {646F4B34-708C-41C2-9F4B-2661849777F3}
Template Icon: Applications/16x16/form_blue.png

 

 

Template Name: Alias
Template Display Name: Alias
Template Full Name: System/Alias
Template ID: {54BCFFB7-8F46-4948-AE74-DA5B6B5AFA86}
Template Icon: Applications/16x16/form_blue.png

 

 

Template Name: Appearance
Template Display Name: Appearance
Template Full Name: System/Templates/Sections/Appearance
Template ID: {F5BC7AE4-F62D-47CD-9AFB-2C9F85D8313B}
Template Icon: Applications/16x16/form_blue.png

 

 

Template Name: Archived item
Template Display Name: Archived item
Template Full Name: System/Archiving/Archived item
Template ID: {BF2B8DA2-3CBA-485D-8F85-3788B8AFBDBF}
Template Icon: Applications/16x16/form_blue.png

 

 

Template Name: Assembly
Template Display Name: Assembly
Template Full Name: Web Forms for Marketers/Assembly
Template ID: {AFD4A7B3-7DB6-4E8F-AFAE-93893A579694}
Template Icon: Applications/16x16/form_blue.png

 

 

Template Name: Audio
Template Display Name: Audio
Template Full Name: System/Media/Unversioned/Audio
Template ID: {D56DB3AA-7373-4651-837E-8D3977A0B544}
Template Icon: people/16x16/loudspeaker.png

 

 

Template Name: Audio
Template Display Name: Audio
Template Full Name: System/Media/Versioned/Audio
Template ID: {E19A2758-F802-4FDC-B497-5FF7B3BAC54B}
Template Icon: people/16x16/loudspeaker.png