Tuesday, October 11, 2011

Changing Field Sharing Settings in Sitecore


So I’ve been working on a C# script which job is to change field value sharing settings on a Sitecore template field from Versioned to Shared, something you can do in the Template Manager application by checking the Shared checkbox and saving.
image
One thought came to me. What will actually happen if I have two language versions of single item with two different values? Consider the following example. I have a versioned image field with two different values in en-US and es-ES:
SNAGHTML20110fe0
So what would be the end result of making the “Image” field shared? It’s actually pretty interesting what happens after you save an item that is a template field definition.

So when a field definition item is saved, DataEngine is firing “ItemSaved” event, and the TemplateEngine has a handler for it which basically checks if either “Unversioned” or “Shared” attributes of the field definition item were changed. If there were, it runs the process of updating the field sharing settings which eventually trickles down to the data provider level where the call is made to a specially designed “FieldSharingConveter” class.

If we are making a versioned field shared, the following method is called:
  • MakeSharedFromVersioned(ID fieldId)
    • MoveDataToSharedFromVersioned(ID fieldId, ID itemId)
      within here there is a SQL query that basically selects the field value from the most recently updated version of the same item.

So basically in order to minimize data loss situations, Sitecore will always use the most recent field value for new shared value. Thought it’s rather interesting.

5 comments:

techphoria414 said...

Alex,

Nice writeup. But what happens if you have multiple language versions as well? I assume the default language is used?

Unknown said...

Thanks!

I thought it uses default language as well, but no, It will use recently updated version in any language version. In this example, if I __updated field of the es-ES version is more recent than other languages, the value from es-ES will be used a new shared field value.

-alex

Ravi Kura said...

Nice post. Very much usefull info.

Can we add a new template field attribute like "IsLanguageTranslatable" similar to "UnVersioned" & "Shared" ?

Thanks,
Ravi Kura.

Ravi Kura said...

Nice post. Very much usefull info.

Can we add a new template field attribute like "IsLanguageTranslatable" similar to "UnVersioned" & "Shared" to all the user defined templates?

Thanks,
Ravi Kura.

Unknown said...

Ravi,

Next major version of Sitecore should be able to address the language sharing better, so something like IsLanguageTranslatable should be possible. No promises on how this will actually work but the engineering is thinking about this.

-alex