Using LinkManager

Using LinkManager

LinkManager

  • UrlOptions Class is used to set LinkManager configuration settings for the current link from code
  • SetUrlOptions Class is used to set the LinkManager configuration settings for a site from code
  • Sitecore.Links.LinkManager Class is used to create links in Sitecore
  • Controls featured in this example:

  • Sitecore Web Control
  • Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • Sitecore.Links.LanguageEmbedding
  • Sitecore.Links.LanguageLocation
  • Sitecore.Links.LinkManager
  • Sitecore.Links.UrlOptions
  • Configuration Settings

    <LinkManager> settings in web.config

  • Allows developers to customize how links are generated
  • Use to override the link provider by setting the type attribute
  • See URL Options page for LinkManager configuration settings in web.config
  • LinkManager C# Web Control Code

    Sitecore.Links.UrlOptions urlOptions = Sitecore.Links.UrlOptions.DefaultOptions.Clone() as Sitecore.Links.UrlOptions;
    Sitecore.Data.Items.Item item = Sitecore.Context.Item;
    if (item == null) return;

    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
    output.Write("C# Web Control Real-time Output");
    output.RenderEndTag();

    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
    //using &#58; for colon and &#63; for question mark and &#160; for space
    output.Write("{0}&#58;&#160;{1}", "Item", item.DisplayName);
    output.RenderEndTag(); //P

    urlOptions.LanguageEmbedding = Sitecore.Links.LanguageEmbedding.Always;
    urlOptions.LanguageLocation = Sitecore.Links.LanguageLocation.FilePath;
    urlOptions.UseDisplayName = false;

    string friendlyUrl = Sitecore.Links.LinkManager.GetItemUrl(item, urlOptions);
    string dynamicUrl = Sitecore.Links.LinkManager.GetDynamicUrl(item);
    string addAspxExt = Sitecore.Links.LinkManager.AddAspxExtension.ToString();
    string includeServerUrl = Sitecore.Links.LinkManager.AlwaysIncludeServerUrl.ToString();
    string linkProvider = Sitecore.Links.LinkManager.Provider.ToString();
    string langLocation = Sitecore.Links.LinkManager.LanguageLocation.ToString();
    string shortenUrls = Sitecore.Links.LinkManager.ShortenUrls.ToString();
    string displayName = Sitecore.Links.LinkManager.UseDisplayName.ToString();

    output.Write("{0}&#58;&#160;{1}", "Url", "<b>" + friendlyUrl + "</b><br/>");
    output.Write("{0}&#58;&#160;{1}", "Dynamic Url", "<b>" + dynamicUrl + "</b><br/>");
    output.Write("{0}&#58;&#160;{1}", "Link Provider", "<b>" + linkProvider + "</b><br/>");
    output.Write("{0}&#58;&#160;{1}", "Language Location", "<b>" + langLocation + "</b><br/>");
    output.Write("{0}&#63;&#160;{1}", "Add Aspx Extension", "<b>" + addAspxExt + "</b><br/>");
    output.Write("{0}&#63;&#160;{1}", "Include Server Url", "<b>" + includeServerUrl + "</b><br/>");
    output.Write("{0}&#63;&#160;{1}", "Shorten URLs", "<b>" + shortenUrls + "</b><br/>");
    output.Write("{0}&#63;&#160;{1}", "Use Display Name", "<b>" + displayName + "</b>");

    output.RenderEndTag(); //Div


    LinkManager C# Web Control Output

    Url: http://tic66/en/Developers/ContentDisplay/LinkManagement/UsingLinkManager
    Dynamic Url: http://tic66/en/Developers/ContentDisplay/LinkManagement/UsingLinkManager
    Link Provider: Sitecore.Links.LinkProvider
    Language Location: FilePath
    Add Aspx Extension? False
    Include Server Url? True
    Shorten URLs? True
    Use Display Name? False