I have created Language selector drop down in Sitecore whenever we select any language that language specific site is opening and page is getting refreshed. So every time its showing drop down index-0 item.

We need to show the language we have selected in drop down. So for storing drop down values can we create session in Sitecore or there is any other way?

  • You can use Sitecore.Context.Language property. This property returns the language for your current request. Sep 26, 2019 at 17:54
  • Yeah - don't use the session for this.
    – Richard Seal
    Sep 26, 2019 at 18:15

1 Answer 1


A good way to keep the language after the section is "Cookie". Sitecore uses sitename#lang cookie to store the language of the website and default language for the website can set in the sitedefinition config by adding language attribute -

  <site name="mysite" language="es-ES" ... />

In your case, I guess everything is working fine except that default selection in the dropdown is not showing the language you are using and it pointing to the first item of the list. you can get that current language from the cookie(if you are using a cookie to store the selected language) or you can check the current request language. A better and recommended way is to use the language from the current request. below is a sample code to make the select box language selectable with current language -

@{ string selected = string.Empty;
    foreach (var regionLang in languageList)
        // Get the context language to compare it with the list of language and make it selected
        var lang = Sitecore.Globalization.Language.Parse(regionLang.Language_Name);
        if (Sitecore.Context.Language == lang)
            //if language matches the current context language then the item exists.  There is no further check required if the item exists in that language.
            selected = "selected";
            //if current context language is not equal to language, then a check is necessary to determine if the item exists in that language
            //because if the item does not exist in that language, we do not even want to show that language in the dropdown, if you are not using language fallback
            if (Sitecore.Context.Database.GetItem(Sitecore.Context.Item.ID, lang).Versions.Count <= 0)
            selected = string.Empty;

        // can be your custom login to show the language in option
        var urlOptions = Sitecore.Links.LinkManager.GetDefaultUrlOptions();
        urlOptions.UseDisplayName = true;
        urlOptions.LanguageEmbedding = Sitecore.Links.LanguageEmbedding.Always;
        urlOptions.Language = lang;
        urlOptions.AlwaysIncludeServerUrl = true;
        var languageItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Item.ID, lang);
        var url = Sitecore.Links.LinkManager.GetItemUrl(languageItem, urlOptions).Replace(" ", "-").Replace(":443", "");

        <option data-url="@url" @selected>@regionLang.Friendly_Display_Name_For_Language</option>

Updates - As Richard suggested in the comments below - above example is just to handle the problem, it is not recommended to add complete code on the view, a controller rendering a better way to handle this code.

  • If you are going to use that amount of logic to do this, at least put it in the controller. The view should never contain all that.
    – Richard Seal
    Sep 27, 2019 at 11:50
  • hahaha... I know but it just a sample to handle it, copy from my old project :) Sep 27, 2019 at 12:03

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.