Get Fields

Get Fields

Sitecore Field Classes

  • Contain convenience methods to make it easier to work with Sitecore field types
  • Implicitly cast fields from the item.Fields collection

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Collections.FieldCollection
  • Sitecore.Context
  • Sitecore.Data.Fields.Field
  • Sitecore.Data.Items.Item
  • Sitecore.Web.UI.WebControls.FieldRenderer

Fields C# Web Control Code

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

Sitecore.Context.Item.Fields.ReadAll();
Sitecore.Collections.FieldCollection fields = item.Fields;
if (fields == 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}", "Parent Item", item.DisplayName);
output.RenderEndTag(); //P

foreach (Sitecore.Data.Fields.Field field in fields)
{
    //remove system fields and empty fields
    if (!field.Name.StartsWith("__") && !string.IsNullOrEmpty(field.Value))
    {
         if (field.Name == "Image")
output.Write("{0}&#58;&#160;{1}", field.Name, Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "image", "mw=220") + "<br/>");
         else
output.Write("{0}&#58;&#160;{1}", field.Name, "<b>" + field.Value + "</b><br/>");
    }
}
output.RenderEndTag(); //Div


Fields C# Web Control Output

TeaserTitle: Template Fields
Abstract: Overview page that contains internal links to data on shared fields, standard fields, template fields, and field properties in Sitecore
TeaserLink: 
Menu Title: Template Fields
Title: Template Fields
TeaserImage:
Image:
Breadcrumb Title: Template Fields
Menu Tooltip: Template Fields
Theme: Theme1