Get Field Values

Get Field Values

Field Values

  1. item["field"];
  2. item.Fields["field"].Value;
  3. item.Fields["field"].ToString();
  4. GetFieldValue("field");

Indexer

  • string text = item["field"]
  • Accesses the raw value of the field
  • Returns an empty string if field value is null or field does not exist
  • Useful for simple field types but not by most List Types or Link Types
  • Does not generate inline editing controls for the Page Editor

FieldsCollection

  • string value = item.Fields["field"]. OR item.Fields["field"].Value
  • Returns null when field id is null or field value does not exist
  • Test for null or place code within a try catch block

GetFieldValue Method

  • GetFieldValue("field");
  • Gets the field from the current data source

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.Item

C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == 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(); //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);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

//using string array to store values from four ways to get field value
string[] field = new string[4];
field[0] = item["title"]; //item["field"]
field[1] = item.Fields["menu title"].ToString(); //item.Fields["field"].ToString();
field[2] = item.Fields["menu tooltip"].Value; //item.Fields["field"].Value;
field[3] = GetFieldValue("breadcrumb title"); //GetFieldValue("field");

if (!string.IsNullOrEmpty(field[0]))
    output.Write("{0}&#58;&#160;{1}", "Title field from item['field']", "<b>" + field[0] + "</b><br/>");
if (!string.IsNullOrEmpty(field[1]))
    output.Write("{0}&#58;&#160;{1}", "Menu Title from item.Fields['field'].Value", "<b>" + field[1] + "</b><br/>");
if (!string.IsNullOrEmpty(field[2]))
    output.Write("{0}&#58;&#160;{1}", "Menu Tooltip from item.Fields['field'].ToString()", "<b>" + field[2] + "</b><br/>");
if (!string.IsNullOrEmpty(field[3]))
    output.Write("{0}&#58;&#160;{1}", "Breadcrumb Title from GetFieldValue('field')", "<b>" + field[3] + "</b>");

output.RenderEndTag(); //Div

C# Web Control Real-time Output

Item: Get Field Values

Title field from item['field']: Get Field Values
Menu Title from item.Fields['field'].Value: Get Field Values
Menu Tooltip from item.Fields['field'].ToString(): Get Field Values
Breadcrumb Title from GetFieldValue('field'): Get Field Values
menu title


Sources