Get Template Sections

Get Template Sections

Template Sections

  • Used to group related fields in the Content Editor to improve usability
  • Provide an accordion display for fields in the Content Editor instead of one large listing

Section Guidelines

  • Each data template contains one or more sections
  • Template fields are organized by template sections
  • All template fields must exist within a section
  • Developers do not reference the section name when accessing template fields from code

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
  • Sitecore.Data.Items.TemplateSectionItem

Template Sections 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 template = db.GetTemplate("TIC/PageTypes/TIC-CodeSnippet");
if (template == null) return;

Sitecore.Data.Items.TemplateSectionItem[] sections = template.GetSections();
if (sections == 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}: {1}", "Template", template.DisplayName);
output.RenderEndTag(); //P

foreach (Sitecore.Data.Items.TemplateSectionItem section in sections)
{
    output.Write("{0}&#58;&#160;{1}", "Section", "<b>" + section.DisplayName + "</b><br/>");
}

output.RenderEndTag(); //Div

Template Sections C# Web Control Output

Template: TIC-CodeSnippet

Section: Code Snippet

Sources