protected void MapToLinkModel(Link link, LinkField linkField, SitecoreFieldConfiguration config) { link.Anchor = linkField.Anchor; link.Class = linkField.Class; link.Style = linkField.GetAttribute("style"); link.Text = linkField.Text; link.Title = linkField.Title; link.Target = linkField.Target; link.Query = HttpUtility.UrlDecode(linkField.QueryString); switch (linkField.LinkType) { case "anchor": link.Url = linkField.Anchor; link.Type = LinkType.Anchor; break; case "external": link.Url = linkField.Url; link.Type = LinkType.External; break; case "mailto": link.Url = linkField.Url; link.Type = LinkType.MailTo; break; case "javascript": link.Url = linkField.Url; link.Type = LinkType.JavaScript; break; case "media": if (linkField.TargetItem == null) { link.Url = string.Empty; } else { global::Sitecore.Data.Items.MediaItem media = new global::Sitecore.Data.Items.MediaItem(linkField.TargetItem); link.Url = global::Sitecore.Resources.Media.MediaManager.GetMediaUrl(media); } link.Type = LinkType.Media; link.TargetId = linkField.TargetID.Guid; break; case "internal": var urlOptions = _urlOptionsResolver.CreateUrlOptions(config.UrlOptions); link.Url = linkField.TargetItem == null ? string.Empty : LinkManager.GetItemUrl(linkField.TargetItem, urlOptions); link.Type = LinkType.Internal; link.TargetId = linkField.TargetID.Guid; link.Text = linkField.Text.IsNullOrEmpty() ? (linkField.TargetItem == null ? string.Empty : linkField.TargetItem.DisplayName) : linkField.Text; break; default: link = null; break; } }
public static string GetGeneralLink(this Item item, string fieldName, bool includeServerName = true) { using (new SecurityDisabler()) { if (item.Fields[fieldName] == null) { return(string.Empty); } LinkField field = (LinkField)item.Fields[fieldName]; if (field.IsMediaLink) { var targetItem = GetFieldValueAsTargetItem(item, fieldName); if (targetItem != null) { var mediaItem = (MediaItem)targetItem; if (mediaItem != null) { return(GetMediaUrl(mediaItem)); } } } if (field.IsInternal) { if (field.TargetItem == null) { return(string.Empty); } else { return(field.TargetItem.GetFriendlyUrl(includeServerName, field.GetAttribute("goalId"))); } } else { return(field.Url); } } }