Using Field Class

Using Field Class

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.Field
  • Sitecore.Data.Items.Item
  • Sitecore.Links.LinkManager

Using Field Class C# Web Control Code

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

//get List Fields metadata item from context database using item guid
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item listItem = db.GetItem("{E3B7CC3C-8C55-4401-A3C9-0FEA9CDA3E9C}");
if (listItem == null) return;

Sitecore.Data.Fields.Field droplinkFld = listItem.Fields["example droplink"];
if (droplinkFld == null) return;

Sitecore.Data.Items.Item targetItem = db.GetItem(droplinkFld.Value);
if (targetItem == 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("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}: ", "Selected Field Value");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, Sitecore.Links.LinkManager.GetItemUrl(targetItem));
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
output.Write("{0}", targetItem.DisplayName);
output.RenderEndTag();  //A
output.RenderEndTag(); //Div


Using Field Class C# Web Control Output

Selected Field Value: Context Device

Notes

  • This code snippet will also work with DropTree and Grouped Droplink controls