0

I'm using sitecore 9.3. I set en as fallback language for en-IE. I have enabled field level fallback on the template level.

I have item with 2 fields. For en-IE:

  • Text_1 - is empty, so value from en is displayed
  • Text_2 - has value "Text_2 en-IE"

field fallback value

How can I get fallback value for Text_1 field? All solutions I have found are related to item level fallback and involve using Sitecore.Data.Items.Item.GetFallbackItem() to get whole fallback item, however in my case I have only one field with fallback value and above method returns null.

I have tried different combinations of field.GetValue(bool allowStandardValue, bool allowDefaultValue, bool allowFallbackValue, bool allowInheritValue, bool allowInnerValue):

Language lang = Language.Parse("en-IE");
Item item_ = master.GetItem("DE0A175C-AB8B-4521-874D-10C7F3F6943C", lang);
Field text_1_field = item_.Fields["Text_1"];
bool contains_fallback_value = text_1_field.ContainsFallbackValue;
string value = text_1_field.Value;
string value_1 = text_1_field.GetValue(false, false, true);
string value_2 = text_1_field.GetValue(false, false, true, false, false);
string value_3 = text_1_field.GetValue(true, true, true, true);

but I haven't got correct field value:

  • contains_fallback_value equals false
  • value equals ""
  • value_1 equals null
  • value_2 equals null
  • value_3 equals ""

Shouldn't value store value from fallback language (i.e. Text) and contains_fallback_value be true? How can I get fallback value for this field?

6
  • Not exactly. Link you have provided is about item level fallback. I use field level fallback.
    – Andrew
    Feb 9 at 13:11
  • Try forcefully field.GetValue(bool allowStandardValue, bool allowDefaultValue, bool allowFallbackValue, bool allowInheritValue, bool allowInnerValue) get the allowFallbackValue and see if you can get the correct value Feb 9 at 13:39
  • @YogeshSharma, you mean get field value using field.GetValue(false, false, true, false, false)? It returns null
    – Andrew
    Feb 9 at 14:48
  • (true, true, true, true) would be the call. But in reality you will get your field value by just going item["field name"]. There is never a need to call ReadAll() for any scenario outside perhaps serialization.
    – Mark Cassidy
    Feb 9 at 17:12
  • @MarkCassidy, thank you for your answer. (true, true, true, true) returned ""
    – Andrew
    Feb 9 at 17:52

2 Answers 2

0

Can you try adding below code while retrieving field value.

using (new LanguageFallbackFieldSwitcher(true))
 {
   // Retrieving Item's Field Value
 }

Also make sure the database do not contains any entry for the item in the fields table. You can check for the corresponding entry in the db by executing below SQL Query:

Select * from {{db_name.FieldsTable}}
Where ItemId = '{{item_id}}' and Language = '{{language}}' and FieldId = '{{fieldid}}'

In case the entry is there you need to delete that and check that again. This is because fallback value is returned only when value in the field is not changed.

3
  • Thank you for the answer! It worked. text_1_field.Value returned fallback value and text_1_field.ContainsFallbackValue was true.
    – Andrew
    Mar 7 at 12:12
  • One more question. In DB I see 3 tables associated with fields: VersionedFields, UnversionedFields, ArchivedFields. By FieldsTable you mean VersionedFields table?
    – Andrew
    Mar 7 at 12:17
  • ya it depends on the field type you are using..If the field is shared it will be present in SharedFields table, in case the field is versioned it will be there on VersionedFields table and lastly if the field is unversioned it will be there on UnVersionedFields table. you can search by the field id's in these table and delete those rows which have empty value and restart the CMS for the fallback changes to take effect.
    – ckhanna
    Mar 7 at 12:43
0

Since I am not able to get the fallback value using field.GetValue(bool allowStandardValue, bool allowDefaultValue, bool allowFallbackValue, bool allowInheritValue, bool allowInnerValue), I have implemented a workaround.

Language lang = Language.Parse("en-IE");
Item item = master.GetItem("DE0A175C-AB8B-4521-874D-10C7F3F6943C", lang);
Field text_1_field = item.Fields["Text_1"];

if (text_1_field.HasValue == false)
{
    Item itemTemplate = master.GetItem(item.TemplateID, item.Language);
    
    Item templateField = itemTemplate.Children[text_1_field.SectionDisplayName].Children[text_1_field.Name];
    
    if (templateField.Fields["Enable Shared Language Fallback"].Value == "1" || templateField.Fields["Enable Language Fallback"].Value == "1")
    {
        //Field level fallback enabled
        Language fallbackLanguage = Sitecore.Data.Managers.LanguageFallbackManager.GetFallbackLanguage(item.Language, master, item.ID);
        Item fallbackItem = master.GetItem(item.ID, fallbackLanguage);
        String fieldValue = fallbackItem.Fields[text_1_field.Name].Value;
        
        //Further logic
    }
    else
    {
        //Field level fallback disabled
        
        //Further logic
    }

}

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.