Skip to content

Instantly share code, notes, and snippets.

@soen
Last active June 6, 2016 18:38
Show Gist options
  • Save soen/55f3683d0705bd4cf7e48108722b9b9e to your computer and use it in GitHub Desktop.
Save soen/55f3683d0705bd4cf7e48108722b9b9e to your computer and use it in GitHub Desktop.
public static class TemplateItemExtensions
{
public static IEnumerable<TemplateFieldItem> GetFieldsOfSelfAndBaseTemplates(this TemplateItem self)
{
var templateFieldItems = new Dictionary<ID, TemplateFieldItem>();
GetAllFieldsForTemplate(self, templateFieldItems);
return templateFieldItems.Values;
}
private static void GetAllFieldsForTemplate(TemplateItem template, Dictionary<ID, TemplateFieldItem> templateFieldItems)
{
AddFieldsOfTemplate(template, templateFieldItems);
if (template.BaseTemplates.Length <= 0)
return;
foreach (TemplateItem baseTemplate in template.BaseTemplates)
GetAllFieldsForTemplate(baseTemplate, templateFieldItems);
}
private static void AddFieldsOfTemplate(TemplateItem template, Dictionary<ID, TemplateFieldItem> templateFieldItems)
{
foreach (TemplateFieldItem x in template.OwnFields)
{
if (templateFieldItems.ContainsKey(x.ID) == false)
templateFieldItems.Add(x.ID, x);
}
}
}
TemplateItem selectedTemplateItem = Sitecore.Context.ContentDatabase.GetItem(new ID("YourID"));
return selectedTemplateItem.GetFieldsOfSelfAndBaseTemplates();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment