Adding Links

Adding Links

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.MultilistField
  • Sitecore.Data.ID
  • Sitecore.Data.Items.Item
  • Sitecore.Links.LinkManager

Adding Links 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;

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("Selected Field Values");
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];
        if (listItem != null)
        {
string displayName = multilistFld.InnerField.DisplayName;
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, Sitecore.Links.LinkManager.GetItemUrl(listItem));
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
output.Write("{0}", listItem.Name + "<br/>");
output.RenderEndTag();  //A
        }
    }
}
output.RenderEndTag(); //Div


Adding Links C# Web Control Output

Selected Field Values

ManagingContent
ItemLocks