You should be careful when using linkField.Url , as it will not display internal links to Sitecore and Media elements correctly. Instead, you should use Sitecore.Links.LinkManager.GetItemUrl(item) and Sitecore.Resources.Media.MediaManager.GetMediaUrl(item) for them.
It would be better to have a helper (extension) method to return the correct URL for you based on the type of link. Take a look at this post "Sitecore Links with LinkManager and MediaManager ), which has the correct code that you need for this.
For reference:
public static String LinkUrl(this Sitecore.Data.Fields.LinkField lf) { switch (lf.LinkType.ToLower()) { case "internal": // Use LinkMananger for internal links, if link is not empty return lf.TargetItem != null ? Sitecore.Links.LinkManager.GetItemUrl(lf.TargetItem) : string.Empty; case "media": // Use MediaManager for media links, if link is not empty return lf.TargetItem != null ? Sitecore.Resources.Media.MediaManager.GetMediaUrl(lf.TargetItem) : string.Empty; case "external": // Just return external links return lf.Url; case "anchor": // Prefix anchor link with
Using:
Sitecore.Data.Fields.LinkField linkField = item.Fields["Link1"]; lnkMain.NavigateUrl = linkField.LinkUrl();
It would be best to use the <sc:FieldRender> and let Sitecore handle it for you, but it looks like you don't have this option.
jammykam
source share