Link Display

Link Display

Sitecore Links

  • Internal Links are used to link to other items in the Content Tree
  • External Links are used to link to a page on another website
  • Media Links are used to link to PDFs, images and other binary media files
  • Mail Links are used to link to an email address - a new mail message is opened using an email client
  • Anchors are used to create an anchor at the current location as a target for other links
  • Javascript is used to activate a JavaScript function instead of an explicit link

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Database
  • Sitecore.Collections.FieldCollection
  • Sitecore.Data.Fields.Field
  • Sitecore.Data.Fields.FieldTypeManager
  • Sitecore.Data.Fields.LinkField
  • Sitecore.Data.Items.Item
  • Sitecore.Data.Items.MediaItem
  • Sitecore.Globalization.Language
  • Sitecore.Links.LanguageEmbedding
  • Sitecore.Links.LanguageLocation
  • Sitecore.Links.UrlOptions
  • Sitecore.Resources.Media.MediaManager
  • Sitecore.StringUtil
  • Sitecore.Web.UI.WebControls.FieldRenderer

Link Display C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
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.Collections.FieldCollection fields = linkItem.Fields;
if (fields == 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

foreach (Sitecore.Data.Fields.Field field in fields)
{
    if (Sitecore.Data.Fields.FieldTypeManager.GetField(field) is Sitecore.Data.Fields.LinkField)
    {
        output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
        output.Write("{0}: {1}", "Field", field.DisplayName);
        output.RenderEndTag(); //P

        Sitecore.Data.Fields.LinkField link = field;
        string frDisplay = Sitecore.Web.UI.WebControls.FieldRenderer.Render(linkItem, field.Name);

        if(link.LinkType != "javascript" && link.LinkType != "anchor")
        output.Write("{0}&#58;&#160;{1}", "FieldRenderer Display", "<b>" + frDisplay + "</b><br/>");
        
        switch (link.LinkType)
        {
case "mailto":
case "javascript":
case "external":
    if (!string.IsNullOrEmpty(link.Url))
    {
        output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, link.Url);
        output.Write("{0}&#58;&#160;", "LinkField Display");
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
        output.Write("{0}", link.Text + "<br/>");
        output.RenderEndTag();
    }
    break;
case "internal":
    if (!string.IsNullOrEmpty(link.Url))
    {
        output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, link.Url);
        output.Write("{0}&#58;&#160;", "LinkField Display");
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
        output.Write("{0}", link.Text + "<br/>");
        output.RenderEndTag();
    }
     if (link.TargetItem != null)
        {
        //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;", "LinkManager UrlOptions");
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
        output.Write("{0}", link.Text + "<br/>");
        output.RenderEndTag();  //A
    }
    break;

case "media":
        Sitecore.Data.Items.Item mItem = db.GetItem(link.MediaPath);
        if (mItem == null) return;
        Sitecore.Data.Items.MediaItem mediaItem = new Sitecore.Data.Items.MediaItem(mItem);
        string mediaUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem);
        string imageUrl = Sitecore.StringUtil.EnsurePrefix('/', mediaUrl);
        output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, imageUrl);
        output.Write("{0}&#58;&#160;", "LinkField Display");
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
        output.Write("{0}", link.Text);
        output.RenderEndTag();  //A
        break;

case "anchor":
        if (!string.IsNullOrEmpty(link.Text))
        {
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Name, link.Text);
output.Write("{0}&#58;&#160;", "LinkField Display");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
output.Write("{0}", "None - anchors are targets for other links");
output.RenderEndTag();
        }
        break; 
        }        
    }
}
output.RenderEndTag();


Link Display C# Web Control Output

Field: Mailto Link

FieldRenderer Display: Webmaster
LinkField Display: Webmaster

Field: Javascript Link

LinkField Display: JavaScript Link

Field: Internal Link

FieldRenderer Display: Site Directory
LinkField Display: Site Directory
LinkManager UrlOptions: Site Directory

Field: External Link

FieldRenderer Display: Link Management with Sitecore
LinkField Display: Link Management with Sitecore

Field: Anchor Link

LinkField Display: None - anchors are targets for other links

Field: Media Link

FieldRenderer Display: Chipper Boat
LinkField Display:

Sources