i have multi-language sitecore website and i m trying to create a language selector in my site header to change site language .

im trying this code but not working :

string siteurl = string.Empty;
var sitesList = Sitecore.Configuration.Factory.GetSiteInfoList();
var currentSiteName = Sitecore.Context.GetSiteName();
var website = Sitecore.Configuration.Factory.GetSite("mysite");

var myItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Item.ID, website.Language);

using (new Sitecore.Sites.SiteContextSwitcher(website))
    var options = LinkManager.GetDefaultUrlOptions();
    options.AlwaysIncludeServerUrl = true;
    options.SiteResolving = true;
    siteurl = LinkManager.GetItemUrl(myItem, options);

 <a href=@siteurl> @Sitecore.Context.Language</a>

  • What are you trying to do here? Generate a list of URLs that go to the available languages on your site? Feb 27 '19 at 12:15
  • Can you explain what part is not working? What do you expect as your output and what is happening instead? Also, if you have SXA, I believe there is a language selector already available for you to use. Feb 27 '19 at 15:15

Try to get the my item in below way:

var myItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Item.ID, website.Language);

  • i try but not working
    – marwen
    Feb 27 '19 at 11:52

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.