Link Types

Link Types

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Collections.FieldCollection
  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.Field
  • Sitecore.Data.Fields.FieldTypeManager
  • Sitecore.Data.Fields.LinkField
  • Sitecore.Data.Items.Item
  • Sitecore.MainUtil

Link Types C# Web Control Code

//get General Link metadata item from context database using item guid
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item item = db.GetItem("{BF877EFB-4A8B-4F6A-8198-415C1DAAD1C5}");
if (item == null) return;

Sitecore.Collections.FieldCollection fields = item.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; 
output.Write("Link Type: <b>" + link.LinkType + "</b><br/>"); 
output.Write("Link is Internal Link? <b>" + link.IsInternal.ToString() + "</b><br/>"); 
if (link.IsInternal) output.Write("Internal Path: <b>" + link.InternalPath + "</b><br/>"); 
output.Write("Link is Media Link? <b>" + link.IsMediaLink.ToString() + "</b><br/>"); 
if (link.IsMediaLink) output.Write("Media Path: <b>" + link.MediaPath + "</b><br/>"); 
if (link.LinkType == "anchor") output.Write("Anchor: <b>" + link.Anchor + "</b><br/>"); 
if (!System.String.IsNullOrEmpty(link.Class)) output.Write("HTML Class: <b>" + link.Class + "</b><br/>"); 
if (!System.String.IsNullOrEmpty(link.Target)) output.Write("HTML Target: <b>" + link.Target + "</b><br/>"); 
if (!System.String.IsNullOrEmpty(link.QueryString)) output.Write("Query String: <b>" + link.QueryString + "</b><br/>"); 
output.Write("Root Tag: <b>" + link.Root + "</b><br/>"); 
output.Write("Target ID: <b>" + link.TargetID.ToString() + "</b><br/>"); 
if(link.TargetItem !=null) output.Write("Target Item: <b>" + link.TargetItem.ToString() + "</b><br/>"); 
if (!System.String.IsNullOrEmpty(link.Text)) output.Write("Link Text: <b>" + link.Text + "</b><br/>"); 
if (!System.String.IsNullOrEmpty(link.Title)) output.Write("Link Title: <b>" + link.Title + "</b><br/>"); 
output.Write("Link Url: <b>" + link.Url + "</b><br/>"); 
output.Write("Link Value Length: <b>" + link.Value.Length.ToString() + "</b><br/>"); 
output.Write("XML Document Element: <b>" + link.Xml.DocumentElement.Name + "</b>");   
    }
}
output.RenderEndTag(); //Div


Link Types C# Web Control Link Types Output

Field: Mailto Link

Link Type: mailto
Link is Internal Link? False
Link is Media Link? False
Root Tag: link
Target ID: {00000000-0000-0000-0000-000000000000}
Link Text: Webmaster
Link Title: Webmaster email
Link Url: mailto:cwnelson@theinsidecorner.com
Link Value Length: 119
XML Document Element: link

Field: Javascript Link

Link Type: javascript
Link is Internal Link? False
Link is Media Link? False
Root Tag: link
Target ID: {00000000-0000-0000-0000-000000000000}
Link Text: JavaScript Link
Link Title: javascript link
Link Url: javascript:SitecoreNET.com
Link Value Length: 217
XML Document Element: link

Field: Internal Link

Link Type: internal
Link is Internal Link? True
Internal Path: /sitecore/content/Home/SiteDirectory.aspx
Link is Media Link? False
Root Tag: link
Target ID: {B51D4FF8-4336-45DF-8DFF-8307C4870F03}
Target Item: Sitecore.Data.Items.Item
Link Text: Site Directory
Link Title: Site Directory
Link Url: /Home/SiteDirectory.aspx
Link Value Length: 173
XML Document Element: link

Field: External Link

Link Type: external
Link is Internal Link? False
Link is Media Link? False
HTML Target: _blank
Root Tag: link
Target ID: {00000000-0000-0000-0000-000000000000}
Link Text: Link Management with Sitecore
Link Url: http://www.sitecore.net/en/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2010/11/Link-Management-with-Sitecore.aspx
Link Value Length: 223
XML Document Element: link

Field: Anchor Link

Link Type: anchor
Link is Internal Link? False
Link is Media Link? False
Anchor: AnchorDemo
Root Tag: link
Target ID: {00000000-0000-0000-0000-000000000000}
Link Text: Anchor Demo
Link Title: anchor demo
Link Url: AnchorDemo
Link Value Length: 102
XML Document Element: link

Field: Media Link

Link Type: media
Link is Internal Link? False
Link is Media Link? True
Media Path: /sitecore/media library/Images/Banners/Transportation/Boat4
Root Tag: link
Target ID: {77033960-E040-4741-8EAA-3F5E80515FFB}
Target Item: Sitecore.Data.Items.Item
Link Url: /Images/Banners/Transportation/Boat4
Link Value Length: 122
XML Document Element: link