2

My sitecore item contains dropdown for City, state, category etc. It's all in multiple languages but on sitecore item dropdown, it's appearing in english only. I've created computed index field for all referenced dropdown. It's indexing only english language content for all languages. Below is my code to get refernce field.

 internal static Item GetTargetItemsFromField(this Item item, ID fieldId)
    {
        if (item.Fields.Contains(fieldId))
        {
            ReferenceField field = item.Fields[fieldId];
            if (field != null && field.TargetItem != null)
            {
                return field.TargetItem;
            }
        }

        return null;
    }

Debug mode: enter image description here

Code is working fine but this code (item.Fields[fieldId]) return english language always. So it's indexing english content only.

I am using below code for language in config file:

 <field fieldName="_language" 
        storageType="YES" 
        indexType="TOKENIZED"    
        vectorType="NO" 
        boost="1f" 
        type="System.String"
        settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
              <analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
  </field>

As per luke output it's indexing only english content for all other language.

How to fix this code to return all language content?

2
  • Do you have other language versions of target Item means City, state? if yes, check your current language context? it should be language specific, check which language is resolving in the current request, is it fr-ca? share your page URl of current request.. Commented May 27, 2017 at 12:01
  • I provided you with and answer, but am still wondering why you would put this information in a computed field (could be me still in holiday mode however..)
    – Gatogordo
    Commented May 29, 2017 at 7:39

1 Answer 1

3

The request for the targetitem does not take into account the language of the item the field is on:

public Item TargetItem
{
  get
  {
    return this.Database.Items[this.Path];
  }
}

This means that it just uses the context language. Your computed index field code is run in the context of an index update, and that will be in an English context. To solve your issue you could use a LanguageSwitcher. You can find some information here: https://ctor.io/correctly-switching-sitecore-contextes/ but you actually just need a using statement around the code:

using (new Sitecore.Globalization.LanguageSwitcher(item.Language.Name))
{
    ...
}

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.