Literal Treelist

Literal Treelist

Literal Controls

  • Literal controls contain HTML tags and other markup elements
  • Server controls contain ASP.NET classes that generate output dynamically on the page

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

C# Web Control Code

//get List Fields metadata item from context database using item guid
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item item = db.GetItem("{7246223A-BFB0-41F9-8A91-1E7D931CDC43}");
if (item == null) return;

Sitecore.Data.Fields.MultilistField treelistFld = item.Fields["example treelist"];
if (treelistFld == null) return;

Sitecore.Data.Items.Item[] items = treelistFld.GetItems();
if (items == null) return;

Sitecore.Web.UI.HtmlControls.Literal tlDisplay = new Sitecore.Web.UI.HtmlControls.Literal();
if (tlDisplay == 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

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}{1}", treelistFld.InnerField.DisplayName, " Selected Items");
output.RenderEndTag(); //P

foreach (Sitecore.Data.Items.Item listItem in items)
{
    tlDisplay.Text += ("<b>" + listItem.DisplayName + "</b><br/>");
}
output.Write("{0}", tlDisplay.Text);
output.RenderEndTag(); //Div

C# Web Control Real-time Output

Item: List Fields

Example Treelist Selected Items

Adding Content
Hidden Items
Verifying Workflow