8

Given that a data template can inherit from other data templates, what happens if the same field name is used in two separate inherited data templates?

4 Answers 4

12

Sitecore stores fields for templates as individual Items. As they are Items, they have a unique ID that can identify them. When Sitecore stores the values of fields for an item (most commonly in the SQL database), it references them via this unique ID rather than the field name.

Because of this, this means that a Sitecore item can inherit from multiple templates that share the same name for fields of theirs, and it can read / write to each of these fields without conflict. You can even have multiple fields with the same name in the same template, though this is not advised.

So, though there is no conflict there, it is very common for developers to access fields for an item via the name, not by the ID, e.g.:

 myItem.Editing.BeginEdit();
 myItem["CommonFieldName"] = "Set this value";
 myItem.Editing.EndEdit();

So what happens in this case? How does Sitecore know what field to update?

In cases such as this, Sitecore will pick the field based on this order of preference:

  • If there is a field on the direct template of the item with that name, it is used. The first matching field-name that is sorted highest (e.g. in the highest section and highest within that section) is selected.
  • If not, then it will iterate over the base templates in the order they are set in the BaseTemplates field and pick the first field with a matching name belonging to one of these templates, using the same sorting logic as above.

This logic is performed by the DoGetField method of the Template class.

If you want to access a specific field, you can use the ID instead:

 myItem.Editing.BeginEdit();
 myItem["{51F6F1D3-D8C6-427B-ADB8-AA3467B3E8DB}"] = "Set this value";
 myItem.Editing.EndEdit();
3

The actual behavior is undefined. More concretely, two fields with identical names will be rolled up into the field collection of the item, and each can be referenced independently via their unique IDs.

However, many Sitecore systems and APIs use only the field name for referencing fields, so you have no guarantees as to which field will actually be used in any given situation.

For example, while I have not tested the following scenario, I imagine that any changes to your templates' field section ordering could affect which duplicate field is used when displaying content on the front-end, so you could easily find yourself in strange situations where a simple field re-ordering could change behavior.

3

It's gonna be two different fields on the resulting item/template because fields are actually Items and technically their Unique ID is Guid, not the Name.

P.S. you could have installed a Sitecore to check that.

P.S.S. I have read through the comments of other people and I would agree that there might be unexpected causes if you access your item fields by name in the code.

I would like to highlight that this is not recommended practice.

You may want to use some mapping frameworks like GlassMapper to avoid those kind of issues (it maps by fieldId as well)

1

I have created "template one" & "template two" which contains a field with same name (i.e. "filedname")
Then, "combined template" template is created and "template one" & "template two" are used as base templates for that.

enter image description here

Then, "combined item" is created under the /home item by using the "combined template"

enter image description here

As you can see, both the fields from both base templates exists in the "combined item"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.