Access field values

Abstract

How to access field values using APIs.

You can use the APIs described in this section to read and write field values. Excluding the Attachment system field type used to store binary data for media items. Sitecore stores all field values as text. You can access any field as a simple text value, or use specialized classes in the Sitecore.Data.Fields namespace.

Some field types consist of a simple text value. Checkbox field stores the one character (“1”) when selected. Rich Text Editor (RTE) fields contain XHTML. Numerous field types contain the ID of a single item or multiple IDs separated by pipe characters (“|”). Other field types contain XML or data in proprietary formats.

Tip

To determine the string format of any field type, open in the Content Editor, select an item containing the field, and then view raw field values.

You can access the string value of any field by using the collection exposed by the Sitecore.Data.Items.Item class. For example, to access the value of the Title field in the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

string title = home["title"];

Important

If the specified field does not exist, the collection exposed by Sitecore.Data.Items.Item returns an empty string, never Null.

You can update the value of any field by using the collection exposed by the Sitecore.Data.Items.Item class. For example, to update the value of the Title field in the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

home.Editing.BeginEdit();

home["title"] = "//TODO: replace with appropriate value";

home.Editing.EndEdit();

You can clear any field by setting its value to an empty string by using the collection exposed by the Sitecore.Data.Items.Item class. For example, to clear the Title field in the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

home.Editing.BeginEdit();

home["title"] = String.Empty;

home.Editing.EndEdit(); 

Important

Clearing the value of a field does not reset that field to its standard value. For instructions to reset a field to its standard value, see the section How to Reset a Field to Its Standard Value. You cannot set a field value to Null.

Alternatively, you can access any field as an instance of the Sitecore.Data.Fields.Field class by using the Sitecore.Data.Items.Item.Fields property. For example, to access the Title field in the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Fields.Field titleField = home.Fields["title"];

if(titleField!=null)

{

string title = titleField.Value;

}

Important

If you access the field by its name (item.Fields[System.String]) and it does not exist in the item, then the Sitecore.Data.Items.Item.Fields collection returns Null.

If you access the field by ID (item.Fields[Sitecore.Data.ID]) and it does not exist in the item, then the Sitecore.Data.Items.Item.Fields collection returns a “new” Sitecore.Data.Fields.Field object which does not belong to the item template.

You can use the Sitecore.Data.Fields.Field.Value property to update the value of a field. For example, to update the value of the Title field in the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Fields.Field titleField = home.Fields["title"];

if(titleField!=null)

{
    home.Editing.BeginEdit();
    titleField.Value = "New Value";
    home.Editing.EndEdit();
}

Sitecore retrieves data only when necessary. To ensure that Sitecore retrieves all field values for an item, invoke the Sitecore.Data.Items.Item.ReadAll method. For example, to iterate the fields of the context item:

Sitecore.Data.Items.Item item = Sitecore.Context.Item;

item.Fields.ReadAll();

foreach (Sitecore.Data.Fields.Field field in item.Fields)
{
    // Process field
}