Show Droplink

Show Droplink

Droplinks

  • The output from a Droplink is a single link item
  • Displays items for the link target to the business user as a simple list
  • Contains the TargetItem framework, which is included with link controls but not list controls
  • Use LinkManager.GetItemUrl("targetItem") to populate the href for the anchor
  • Use the TargetItem.Name property for displaying the link text

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.ReferenceField
  • Sitecore.Data.Items.Item

Show Droplink C# Web Control Code

//get Link Fields metadata item from context database using item guid
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item item = db.GetItem("{E3B7CC3C-8C55-4401-A3C9-0FEA9CDA3E9C}");
if (item == null) return;

Sitecore.Data.Fields.ReferenceField droplinkFld = item.Fields["example droplink"];
if (droplinkFld == 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 (droplinkFld.TargetItem != null)
{
    Sitecore.Data.Items.Item targetItem = droplinkFld.TargetItem;
    string displayLink = System.String.Format(@"<a href={0}>{1}</a>", Sitecore.Links.LinkManager.GetItemUrl(targetItem), targetItem.DisplayName);
    output.Write("{0}&#58;&#160;{1}", droplinkFld.InnerField.DisplayName + " Field Value", "<b>" + displayLink + "</b>");
}
output.RenderEndTag(); //Div


Show Droplink C# Web Control Output

Example Droplink Field Value: Context Device

Notes

  • This snippet uses the context database instead of specifying the database to use - presentation components that retrieve content for display almost always access the context database rather than referencing a database by name
  • This snippet uses a fields collection to reference the field