Search code examples
sitecoresitecore8

Getting Layouts in page url


I am working on Sitecore 8.1 and I am trying to get value from genral link field.

I am facing below issues:-

  1. If i am using linkfield.url- It's generates null value, I did check in sitecore and there is a value in the field.
  2. If I am using linkfield.getfriendlyurl()- Url is genrating "http://hostname/layouts/language-code/page.aspx" it contain "layouts" which is not in content tree. Only one thing I can see here if I am removing "var abc = item.Language.Name;" this language code then i am not getting layouts in url.

In my case, I have to add the language in URL. Please see below code for reference.

  Sitecore.Data.Fields.LinkField linkField = item.Fields["Link"];
  var opts = (UrlOptions)UrlOptions.DefaultOptions.Clone();
  hlLink.NavigateUrl = item.Language.Name + Sitecore.Links.LinkManager.GetItemUrl(linkField.TargetItem,opts);

Any help would be appreciated.


Solution

  • Please try below given code. I think you are missing "/" with language code and because of this it's generating URL with layouts (Physical folder name).

    Sitecore.Data.Fields.LinkField linkField = item.Fields["Link"];
      var opts = (UrlOptions)UrlOptions.DefaultOptions.Clone();
      hlLink.NavigateUrl = "/"+ item.Language.Name + Sitecore.Links.LinkManager.GetItemUrl(linkField.TargetItem,opts);