Show Droptree

Show Droptree

Droptree

  • The output from a Droptree is a single link item
  • Displays items for the link target to the business user as a hierarchy of nested items multiple levels deep, with items for the link target selectable at any level, such as child items, grandchildren, great-grandchildren, etc.
  • 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 Droptree 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 droptreeFld = item.Fields["example droptree"];
if (droptreeFld == 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 (droptreeFld.TargetItem != null)
{
    Sitecore.Data.Items.Item targetItem = droptreeFld.TargetItem;
    string displayLink = System.String.Format(@"<a href={0}>{1}</a>", Sitecore.Links.LinkManager.GetItemUrl(targetItem), targetItem.DisplayName);
    output.Write("{0}&#58;&#160;{1}", droptreeFld.InnerField.DisplayName + " Field Value", "<b>" + displayLink + "</b>");
}
output.RenderEndTag(); //Div


Show Droptree C# Web Control Output

Example Droptree Field Value: Devices