Show Selected Items

Show Selected Items

Controls featured in this example:

  • Sitecore User Control (.ascx)
  • Sitecore Web Control (.cs)

Classes featured in this example:

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

Show Selected Items Markup in ASCX file

<div style="font-weight: bold; width: 100%; clear: both">
    <asp:Label ID="Label1" runat="server" />
</div>
<div style="font-size: x-small; width: 100%; clear: both">
    <p>
        <asp:Label ID="Label2" runat="server" /></p>
       <p> <asp:Literal ID="Literal1" runat="server" /></p>
</div>

Show Selected Items C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == null) return;

//get List Fields metadata item from context database using item path
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item contentRoot = db.GetItem(Sitecore.ItemIDs.ContentRoot);
if (contentRoot == null) return;

Sitecore.Data.Items.Item listsItem = db.GetItem(contentRoot.Paths.Path + "/TIC-Meta/FieldsDemos/List Fields");
if (listsItem == null) return;

Sitecore.Data.Items.Item[] items = null;
Sitecore.Data.Fields.MultilistField multilistFld = null;

if (!string.IsNullOrEmpty(listsItem["example multilist"]))
{
    multilistFld = listsItem.Fields["example multilist"];
    if (multilistFld == null) return;

    items = multilistFld.GetItems();
}

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(); //Div

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);
string displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using &#58; for colon and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Item", displayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Ul);
if (multilistFld.TargetIDs != null && multilistFld.TargetIDs.Length > 0)
{
    foreach (var selectedItem in items)
    {
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
        output.Write("{0}", "<b>" + selectedItem["Title"] + "</b>");
        output.RenderEndTag(); //Li
    }
}
output.RenderEndTag(); //Ul
output.RenderEndTag(); //Div


Show Selected Items C# User Control Code

if (!IsPostBack)
{
Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == null) return;

//get List Fields metadata item from context database using item path
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item contentRoot = db.GetItem(Sitecore.ItemIDs.ContentRoot);
if (contentRoot == null) return;

Sitecore.Data.Items.Item listsItem = db.GetItem(contentRoot.Paths.Path + "/TIC-Meta/FieldsDemos/List Fields");
if (listsItem == null) return;

Sitecore.Data.Items.Item[] items = null;
Sitecore.Data.Fields.MultilistField multilistFld = null;

if (!string.IsNullOrEmpty(listsItem["example multilist"]))
{
    multilistFld = listsItem.Fields["example multilist"];
    if (multilistFld == null) return;

    items = multilistFld.GetItems();
}

Label1.Text = "C# User Control Real-time Output";
string displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using &#58; for colon and &#160; for space
Label2.Text = ("<p><b>Item&#58;&#160;" + displayName + "</b></p>");

if (multilistFld.TargetIDs != null && multilistFld.TargetIDs.Length > 0)
{
    Literal1.Text += "<ul>";
    foreach (var selectedItem in items)
    {
        Literal1.Text += "<li><b>" + selectedItem["Title"] + "</b></li>";
    }
    Literal1.Text += "</ul>";
}
}

Show Selected Items C# Web Control Output

  • Managing Content
  • Item Locks

Notes

  • This code uses a fields collection to reference the field
  • This code also applies to a Checklist, Treelist, or TreelistEx field