0

I have created a custom Sitecore field from Sitecore.Shell.Applications.ContentEditor.NameLookupValue. But even after changing the language of the item where the custom field is added, context language, as well as the item language, is always coming as en.

So I am not able to get the value of the item field in other languages. Sitecore.Context.ContentDatabase.GetItem(this.ItemID) is always giving values of en language I have tried with httpcontext.current.request["sclanguage"]. But it is always giving the previous language.

2 Answers 2

1

You can use a LanguageSwitcher to change the language:

e.g.

using (new LanguageSwitcher("OTHER LANGUAGE CODE"))
{
    var item = Sitecore.Context.Database.GetItem(this.ItemID);
    var field = item.Fields["FieldName"];
}

Getting the field value without a language switcher will always use the current context language. If the context language is en - it will get the en language version.

Make sure that in your data template you have not ticked Unversioned or Shared

3
  • I tried this as well But how to get the field language in code while changing the item language Commented Sep 9, 2023 at 3:49
  • Your field would be getting rendered on a page present within a Sitecore site. You should be looking at Context Language in code, something like this Sitecore.Context.Language, to get the language in code.
    – Ghan
    Commented Sep 9, 2023 at 5:56
  • Sitecore.context.language is always en Commented Sep 9, 2023 at 22:02
0

Try this

var lang = Sitecore.Globalization.Language.Parse("fr");
var item = Sitecore.Context.ContentDatabase.GetItem(this.ItemID, lang );

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.