Get Template Fields

Get Template Fields

Template Fields

  • The name column is used to specify the field name
  • The type column is used to specify the field type
  • The source column is used to specify a GUID or item path for the item in the Content Tree referenced by the field 

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.TemplateFieldItem

Template Fields 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.TemplateFieldItem[] fields = template.Fields;
if (fields == 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.TemplateFieldItem field in fields)
{

    if (!field.Name.StartsWith("__"))
        output.Write("{0}&#58;&#160;{1}", "Field", "<b>" + field.Name + " - " + field.Type + "</b><br/>");
}

output.RenderEndTag(); //Div


Template Fields C# Web Control Output

Template: TIC-CodeSnippet

Field: Info - Rich Text
Field: Sources - Rich Text
Field: CSharp Web Control Code - Rich Text
Field: Image - Image
Field: Menu Title - Single-Line Text
Field: Tiny URL - Single-Line Text
Field: Title - Single-Line Text
Field: CSharp User Control Code - Rich Text
Field: Abstract - Multi-Line Text
Field: Web Control Demo - Rich Text
Field: Menu Tooltip - Single-Line Text
Field: QR Code - Image
Field: Theme - Droplist
Field: Sublayout Demo - Rich Text
Field: Breadcrumb Title - Single-Line Text
Field: XSLT Demo - Rich Text
Field: Related Topics - Multilist
Field: XSLT Rendering Code - Rich Text