Show Grouped Droplist

Show Grouped Droplist

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

Show Grouped Droplist 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.GroupedDroplistField grpDlistFld = item.Fields["example grouped droplist"];
if (grpDlistFld == 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 (!System.String.IsNullOrEmpty(grpDlistFld.Value))
{
    output.Write("{0}&#58;&#160;{1}", grpDlistFld.InnerField.DisplayName + " Field Value", "<b>" + grpDlistFld.Value + "</b>");
}
output.RenderEndTag(); //Div


Show Grouped Droplist C# Web Control Output

Example Grouped Droplist Field Value: ContentManagement