Create Link to Sitecore Item Programmatically using Link Manager

As a Sitecore developer, we would always need to Link to different Sitecore Items, Media Items. So How Does Sitecore handles the Internal and External Link and How do we resolve the correct URL Programmatically? This article explains on How to Create Link to Sitecore Item Programmatically using Link Manager

public string ResolveSitecoreURL(Sitecore.Data.Items.Item item)
{
  return Sitecore.Links.LinkManager.GetItemUrl(item);
}

By Using LinkManager we will be able to resolve a URL to Sitecore Item. The First step is to get the Sitecore item and Pass it to the Method ResolveSitecoreURL(item) and pass the item to it.

How to Get Absolute URL of an Item in Sitecore

If you need the Absolute URL or a fully qualified URL including the domain name then Sitecore LinkManager provides overloading of URLOptions.

var options = LinkManager.GetDefaultUrlOptions();
options.AlwaysIncludeServerUrl = true;

Item homeItem = Sitecore.Context.Database.GetItem("/sitecore/Content/Home");
string link = LinkManager.GetItemUrl(homeItem, options);

You could control the behaviour of resolving the Links in Sitecore by making few changes in the web.config file. Change the alwaysIncludeServerUrl value to true if you need the absolute URL and make it false if you need only a relative URL.

<linkManager defaultProvider="sitecore">
  <providers>
    <clear />
    <add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" alwaysIncludeServerUrl="true" ... />
  </providers>
</linkManager>

You will not be able to fetch the Media Item using the above LinkManager.GetItemUrl() Method. Sitecore has a separate API to fetch the Media URLs.

public string ResolveSitecoreMediaURL(Sitecore.Data.Items.Item item)
{
  return Sitecore.Resources.Media.MediaManager.GetMediaUrl(item);
}

Like LinkManager had the URLOptions MediaManager comes with MediaUrlOptions where you can control the absolute and relative URLs.

Working with Sitecore LinkFields

If you are working with LinkField then you could create the function as shown below and resolve the correct URLs automatically.

Sitecore.Data.Fields.LinkField linkFld = Sitecore.Context.Item.Fields["Link"];
switch (linkFld.LinkType.ToLower())
{
  case "internal":
    // Internal Links will be Fetched using LinkManager
    return linkFld.TargetItem != null ? Sitecore.Links.LinkManager.GetItemUrl(linkFld.TargetItem) : string.Empty;
  case "media":
    // Media Items will be Fetched using MediaManager
    return linkFld.TargetItem != null ? Sitecore.Resources.Media.MediaManager.GetMediaUrl(linkFld.TargetItem) : string.Empty;
  case "external":
    // Return External Links
    return linkFld.Url;
  case "anchor":
    // Anchors will be Prefixed with a speical character '#'
    return !string.IsNullOrEmpty(linkFld.Anchor) ? "#" + linkFld.Anchor : string.Empty;
  case "mailto":
    // Return the URL if its Mailto
    return linkFld.Url;
  case "javascript":
    // Return the URL if its Mailto
    return linkFld.Url;
  default:
    // Perform a default option if none of the conditions meet
    return linkFld.Url;
}
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like