I use the following block of code to generate the links of media items in a specific language:

    Sitecore.Links.UrlOptions urlOptions = Sitecore.Links.LinkManager.GetDefaultUrlOptions();
    urlOptions.AlwaysIncludeServerUrl = true;
    urlOptions.Language = Language.Parse(item.Lang);
    string lnk = Sitecore.Links.LinkManager.GetItemUrl(pdfItem, urlOptions);

...
linkfield.Url = lnk;

The link generated looks like this:

http://cd5-int.global.com/-/media/Files/Mediaroom/700767.pdf

My problem is it keeps redirecting me to the last language version i clicked on. It seems to be a cache problem because when i click the link opening my media, if the pdf opened in a new window is in the wrong language, i just need to refresh the page and then the pdf appears in the right language.

Did i miss some useful options in my urlOptions object ?

  • Have you tried using the Sitecore.Resources.Media.MediaManager.GetMediaUrl() method to generate your media links instead? – Dan Sinclair Dec 6 at 15:04

For media items you should use the MediaManager to generate links instead of the LinkManager. The MediaManager will return a link to the actual media item.

var urlOptions = new MediaUrlOptions();
urlOptions.Language = Language.Parse(item.Lang)                
var lnk = MediaManager.GetMediaUrl(item);

Try including the LanguageEmbedding option in your UrlOptions:

urlOptions.LanguageEmbedding = LanguageEmbedding.Always;

That should result in a URL that has the language in the querystring, like so:

http://cd5-int.global.com/-/media/Files/Mediaroom/700767.pdf?lang=en

  • Thank you for your help. Unfortunately the language still does not appear in the querystring. Do you know what might cause this problem ? – Slrg Dec 6 at 14:30
  • Do you have different settings specified for your link provider in the config? You can check by going to /sitecore/admin/showconfig.aspx and searching for Sitecore.Links.LinkProvider. The attribute to look for is languageEmbedding. – Dan Sinclair Dec 6 at 15:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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