Get Template

Get Template

Templates

  • Templates appear under the Templates node usually in a folder that identifies the project
  • Sections appear as child items under the template name
  • Fields appear as child items under the name of the section where they were created
  • Field Types and Field Sources are defined in the field item itself
  • The path to the template item starts at the location "/sitecore/Content/Templates"
  • Use Template Folders to organize your templates

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item
  • Sitecore.Data.Items.TemplateItem

Template C# Web Control Code

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

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.TemplateItem template1 = db.GetTemplate("{96C76301-A0EA-4B0D-A092-4BF8EF870B1A}");
if (template1 == null) return;

Sitecore.Data.Items.TemplateItem template2 = db.GetTemplate("TIC/PageTypes/TIC-CodeSnippet");
if (template2 == 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}", "Template By ID", "<b>" + template1.DisplayName + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Template By Path", "<b>" + template2.DisplayName + "</b>");

output.RenderEndTag(); //Div


Template C# Web Control Output

Template By ID: TIC-ConfigSetting
Template By Path: TIC-CodeSnippet