When you need all fields :-)
For performance reasons, Sitecore will not explicitly read fields that have no value, or fields that have only Standard Value defined. The only time you will need to call .ReadAll
is when you plan to loop through the FieldCollection
- pretty much like the scenario you describe above.
For reference:
For the sake of performance, Sitecore will not give you all fields in the FieldCollection in the following code, only fields with explicit values on item level, including empty string:
foreach(Sitecore.Data.Fields.Field field in Sitecore.Context.Item.Fields)
{
// here you will see only fields with values (even empty string) set on item level
// fields with null in them or standard values will not be here.
}
Source: Friday Gotcha: Sitecore.Data.Items.Item.Fields
Reading the template definition
If you are interested in what fields are defined for your item (template), TemplateManager
also has a method to help you out.
Template template = TemplateManager.GetTemplate(new ID(""), Sitecore.Context.Database);
TemplateField[] allFields = template.GetFields(true);
Adding (true)
to the GetFields
call, will instruct TemplateManager
to also return base fields.
Essentially this is what ReadAllFields
does, behind the scenes.
Source: Getting all the fields of a template in Sitecore
On source
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)
{
// TODO: process field
}
From the: Sitecore CMS 6.4 and later Content API Cookbook
Funnily enough, this is not mentioned in the 6.0 cookbook. I would have thought this method was around since the beginning of time - but I've found no earlier mention of it.
product.Fields.All(f => f.Name != field.Name)
. You're welcome for the unsolicited advice! ;P