Using UrlOptions

Using UrlOptions

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.LinkField
  • Sitecore.Data.Items.Item
  • Sitecore.Globalization.Language
  • Sitecore.Links.LanguageLocation
  • Sitecore.Links.LanguageEmbedding
  • Sitecore.Links.LinkManager
  • Sitecore.Links.UrlOptions

Using UrlOptions C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item linkItem = db.GetItem("{BF877EFB-4A8B-4F6A-8198-415C1DAAD1C5}");
if (linkItem == null) return;

Sitecore.Data.Fields.LinkField link = linkItem.Fields["internal link"];
if (link == 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 : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

if (link.TargetItem != null && !string.IsNullOrEmpty(link.Url))
{
    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, link.Url);
    output.Write("{0}: ", "LinkField Display");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
    output.Write("{0}", link.Text + "<br/>");
    output.RenderEndTag();
    string targetUrl = Sitecore.Links.LinkManager.GetItemUrl(link.TargetItem);
    output.Write("{0}&#58;&#160;{1}", "Link Url", "<b>" + link.Url + "</b><br/>");
    output.Write("{0}&#58;&#160;{1}", "Target Url", "<b>" + targetUrl + "</b>");
         
    //alternative to Sitecore.Links.UrlOptions urlOptions = Sitecore.Links.LinkManager.GetDefaultUrlOptions();
    Sitecore.Links.UrlOptions urlOptions = (Sitecore.Links.UrlOptions)Sitecore.Links.UrlOptions.DefaultOptions.Clone();
    urlOptions.AlwaysIncludeServerUrl = false;
    urlOptions.EncodeNames = true;
    urlOptions.EmbedLanguage(Sitecore.Globalization.Language.Current);
    urlOptions.LanguageLocation = Sitecore.Links.LanguageLocation.FilePath;
    urlOptions.LanguageEmbedding = Sitecore.Links.LanguageEmbedding.Always;
    string linkUrlOptions = Sitecore.Links.LinkManager.GetItemUrl(link.TargetItem, urlOptions);

    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, linkUrlOptions);
    output.Write("{0}&#58;&#160;", "<br/><br/>LinkManager UrlOptions");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
    output.Write("{0}", link.Text + "<br/>");
    output.RenderEndTag();  //A

    //output.Write("{0}", linkUrlOptions + "<br/>");
    output.Write("{0}&#58;&#160;{1}", "Link Url", "<b>" + link.Url + "</b><br/>");
    output.Write("{0}&#58;&#160;{1}", "Target Url", "<b>" + linkUrlOptions + "</b>");
}
output.RenderEndTag();  //Div


Using UrlOptions C# Web Control Output

LinkField Display: Site Directory
Link Url: /Home/SiteDirectory.aspx
Target Url: http://tic66/en/SiteDirectory

LinkManager UrlOptions: Site Directory
Link Url: /Home/SiteDirectory.aspx
Target Url: /en/SiteDirectory