Using TargetIDs

Using TargetIDs

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

Using TargetIDs 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 multilistFld = item.Fields["example multilist"];
if (multilistFld == null) return;

string source = multilistFld.InnerField.Source;
Sitecore.Data.Items.Item sourceItem = db.GetItem(source);
if (sourceItem == null) return;

Sitecore.Collections.ChildList children = sourceItem.GetChildren();
if (children == 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);
output.Write("Field: " + multilistFld.InnerField.DisplayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("Full List of Items");
output.RenderEndTag(); //P

foreach(Sitecore.Data.Items.Item child in children)
{
    output.Write("{0}", "<b>" + child.DisplayName + "</b><br/>");
}

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("Selected Items");
output.RenderEndTag(); //P

if (multilistFld.TargetIDs != null && multilistFld.TargetIDs.Length > 0)
{
    foreach (Sitecore.Data.ID id in multilistFld.TargetIDs)
    {
        Sitecore.Data.Items.Item listItem = Sitecore.Context.Database.Items[id];
        output.Write("{0}","<b>" + listItem.DisplayName + "</b><br/>");
    }
}
output.RenderEndTag(); //Div


Using TargetIDs C# Web Control Output

Full List of Items

Item Features
Item Locks
Managing Content
Publishing Content
Video Delivery

Selected Items

Managing Content
Item Locks