1

Problem statement

1) I have a template "Article" with 3 fields "Title" - Single line text "Description" - rich-text "Category" - drop-link

2) I created a computed field for "category" to get the "categoryvalue"

3) I created a category, Article item in English version everything worked fine

4) I create the same item in different language say "Thai"

Issue - I am always getting "CategoryValue" in English even though I have created "Thai" versions of all the items.

What I researched I came across some blogs which says that "Indexing always run in default language" and also being suggested to use Sitecore.Globalization.LanguageSwitcher while writing code for computed index.

What Ii debug While debugging I found that at the time of indexing Sitecore.Context.Language is always English and after using LanguageSwitcher things worked i.e. I am getting language-specific values

What is want to understand

1) Is it correct the computed indexes are always executed in English language?

2) Is applying "sitecore.Globalization.LanguageSwitcher " only solution or is there any configuration which i am missing here?

Code Snippet

Please note I have commented the language switcher via which things are working fine

 public override object ComputeFieldValue(IIndexable indexable)
    {
        Item item = indexable as SitecoreIndexableItem;

        if (item == null || item.Paths.Path.Contains(SearchConstant.StandardValues)) return null;

        GroupedDroplinkField contentTypeField = null;

        if (item.IsDerived(new ID(SearchConstant.BaseArticleTemplateID)) )
        {
            contentTypeField = item.Fields[SearchConstant.ContentTypeField];
        }

        if (contentTypeField == null || contentTypeField.TargetItem == null) return null;

        if (contentTypeField != null && contentTypeField.TargetItem != null)
        //Please note if i dont put LanguageSwitcher then it always give me english content
         //   using (new Sitecore.Globalization.LanguageSwitcher(item.Language.Name))
           // {
                return contentTypeField.TargetItem.Fields[SearchConstant.Name].Value;
            //}

        return null;
    }

Code snippet for Multilist field

public override object ComputeFieldValue(IIndexable indexable)
    {

        Item item = indexable as SitecoreIndexableItem;


        if (item == null)
        {
            return null;
        }
        List<string> featuredTagList = null;
        if ((item.IsDerived(new ID(SearchConstant.BaseArticleTemplateID)))
        {
            MultilistField listField = item?.Fields["FeaturedTags"];
            if (listField?.TargetIDs != null && listField.TargetIDs.Length > 0)
            {
                //using (new Sitecore.Globalization.LanguageSwitcher(item.Language.Name))
                //{
                    var listItems = listField.GetItems();
                    featuredTagList = (from listItem in listItems where listItem?.Fields[SearchConstant.Name] != null let fieldValue = listItem.Fields[SearchConstant.Name].Value select !string.IsNullOrEmpty(fieldValue) ? fieldValue : listItem.Name).ToList();
                //}
            }
        }

        return featuredTagList;
    }
}
  • Can you post code of your computed index field? – Marek Musielak Jan 11 at 11:41
  • @MarekMusielak I have updated my question with a code sample. Please have a look – user36299 Jan 12 at 14:49
1

This is standard Sitecore behavior.

GroupedDroplinkField is a type inheriting from LookupField type.

That field's value is only a Sitecore ID of the target item, without any information about language or anything else.

Your indexable item may use Thai language, but when you call contentTypeField.TargetItem, Sitecore gets the item in your current context language, which for Sitecore backend tasks usually is English.

It's the same like you would call there

var contentTypeItem = item.Database.GetItem(contentTypeField.TargetID)

Sitecore will return English version of the contentTypeItem.

Using LanguageSwitcher in your scenario is a good idea.

You can also use code like the one below, if you don't like LanguageSwitcher in your code:

var contentTypeItem = item.Database.GetItem(contentTypeField.TargetID, item.Language);
  • Does the same thing applies for a multilist field? I have updated the code for multilist field in this case as well I need to use language switcher – user36299 Jan 12 at 16:46
  • It does. If you're running in a Sitecore context where you haven't specified any language, MultilistField.GetItems() will just use the default. If you're running in Sitecore "shell" site context, it will use whichever language is configured for "shell" site. – Marek Musielak Jan 12 at 16:49
  • So can we conclude saying that for computed index the context language will always be shell language and developers should always take care of Language (via switcher or some other way) in case of a multilingual site? – user36299 Jan 12 at 16:51
  • It's not necessarily always a shell language. It depends on indexing strategy you choose and on custom code which you can use to change or publish the items which will be indexed after. But the second part of your conclusion is correct: "When it comes to computed index fields, never assume what your language will be. If you're just using IIndexable item from the parameter, you will get it in right language. But if you're getting something else from database, tell Sitecore which language should be used". – Marek Musielak Jan 12 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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