Getting all the fields of a template in Sitecore

In this blog post I'll explain how you can get all the fields of a template, including the fields of its base templates.

Accessing the fields of a template

If you look at Sitecore's documentation on the TemplateItem class which represents a template item in Sitecore, you'll find that it contains two properties related to accessing the fields provided by the template:

  • TemplateItem.Fields, that gets the fields.
  • TemplateItem.OwnFields, that gets the fields that are defined by the template.

By simply reading the documentation it was my impression, that TemplateItem.Fields would return all fields for the template. This would include the fields specified by the template, along with every field that comes from the base templates that it would inherit from. In addition, TemplateItem.OwnFields would return the fields specified directly on the template itself.

This turned out not to be completely true, since TemplateItem.Fields only returns the fields on the template item, along with the standard values fields, so how do we get all the fields that a template provides?

Getting all the fields

All the things image

In order to get what we need, we can write our own code to get the fields of the template, along with the fields found on all base templates.

I've provided a working code sample down below on how this can be done, including an example on how you would use it:

As far as the implementation goes, I should mention that I originally tried to do the implementation using a HashSet<TemplateFieldItem> instead of the Dictionary<ID, TemplateFieldItem>. However, that ended up giving me duplicate entries, which is because the GetHashCode()method has not been overridden within the TemplateItem class. Because of this I needed to use a dictionary with the ID of the template field as key and the TemplateFieldItem as the value. In my point of view, it would have made sense if it had returned the hash code of the ID property, since the field ID uniquely defines the field, so why not put that into use?

As always, if you have any comments or questions, please let me know in the comments section down below.


After I published this blog post, my good colleague Anders Laub told me that there is an easier (and a bit hidden) way to get all the fields of a template - including the ones from the base templates.

By using the TemplateManager you can get the template, whereas the returned Template object exposes a method called GetFields(). Now, if you simply call the method, you will end up getting the fields on just the template, like I originally got. However, the GetFields() method has an overload, which takes in a boolean parameter called includeBaseFields, where you can explicitly say that you want to include all the base fields.

I have provided an updated code sample down below, that will do the same as the custom code I did for the blog post, just a bit more simple and using the standard Sitecore API: