Return to doc.sitecore.com

Valid for Sitecore 5.3.1
9.  Link
Prev Next

The complex field type, Security stores it raw value as text:

<link text="Sitecore" linktype="external" url="http://www.sitecore.net" anchor="" title="go to Sitecore" class="Normal" target="_blank" /> 

Resolve the value by using implicit casting

public static string outLink(Sitecore.Data.Items.Item item, string sFieldName)
{
  System.Text.StringBuilder builder
= new System.Text.StringBuilder();
  
  Sitecore.Data.Fields.LinkField linkField
= item.Fields[sFieldName];
  builder.Append(
"linkField.Anchor = " + linkField.Anchor);
  builder.Append(
"<br />");
  builder.Append(
"linkField.Class = " + linkField.Class);
  builder.Append(
"<br />");
  
/// Link type can be:
  
/// - external (do not fill out TargetItem)
  
/// - internal (fill out TargetItem = item in Sitecore tree)
  
/// - media (fill out TargetItem = item in media library).
  
/// - anchor (do not fill out TargetItem, fill out Anchor + Url).
  
/// - mailto (do not fill out TargetItem, fill out url).
  
/// - javascript (do not fill out TargetItem, fill out Url, e,g. javascript:alert('hello').
  builder.Append("linkField.LinkType = " + linkField.LinkType);
  builder.Append(
"<br />");
  builder.Append(
"linkField.Target = " + linkField.Target);
  builder.Append(
"<br />");      
  
if (linkField.TargetItem != null)
  {
    builder.Append(
"linkField.TargetItem.DisplayName = " + linkField.TargetItem.DisplayName);
    builder.Append(
"<br />");        
  }
  builder.Append(
"linkField.Text = " + linkField.Text);
  builder.Append(
"<br />");
  builder.Append(
"linkField.Title = " + linkField.Title);
  builder.Append(
"<br />");
  builder.Append(
"linkField.Url = " + linkField.Url);

  
return builder.ToString();
}

Result:

linkField.Anchor =
linkField.Class = Normal
linkField.LinkType = external
linkField.Target = _blank
linkField.Text = Sitecore
linkField.Title = go to Sitecore
linkField.Url = http://www.sitecore.net

 


Prev Next