Skip to content

Instantly share code, notes, and snippets.

@RyanABailey
Created October 30, 2014 05:07
Show Gist options
  • Select an option

  • Save RyanABailey/e170c36d2b42ca7f9c7a to your computer and use it in GitHub Desktop.

Select an option

Save RyanABailey/e170c36d2b42ca7f9c7a to your computer and use it in GitHub Desktop.
linkfield to url
/// <summary>
/// Get the URL for a given sitecore linkfield
/// </summary>
/// <param name="lf">sitecore linkfield</param>
/// <returns>web URL</returns>
public static string GetLinkFieldURL(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 # if link if not empty
return !string.IsNullOrEmpty(lf.Anchor) ? "#" + lf.Anchor : string.Empty;
case "mailto":
// Just return mailto link
return lf.Url;
case "javascript":
// Just return javascript
return lf.Url;
default:
// Just please the compiler, this
// condition will never be met
return lf.Url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment