LibrarySites.Banner

All About Standard Values in the Sitecore ASP.NET CMS

This blog post contains information about standard values in the Sitecore ASP.NET web Content Management System (CMS).

Introduction

Standard values provide initial values for all items based on a given data template. Each data template can have a standard values item, where that item can contain a standard value for each field in that data template and each of its base templates. Each data template can have only one standard values items (you can work around this by creating an additional template that inherits from the original template but defines different standard values).

The standard values item is simply a regular item named __Standard Values based on that template. For example, for the data template Sample/Sample Item (/sitecore/templates/Sample/Sample Item), the standard values item is would be /sitecore/templates/Sample/Sample Item/__Standard Values (if such an item exists), where that item uses the Sample/Sample Item data template.

If a field of an item contains its standard value, the Content Editor renders [standard value] in the header above the field.

Standard Values Token Replacement

When you create an item from a data template or a branch template, Sitecore expands tokens such as $name in field values. Of course you can create your own custom tokens. For more information about standard values token replacement, see the links in the Resources section at the bottom of this page.

Default Value

Do not confuse standard values (sometimes referred to as initial values) from default values. Long before standard values, Sitecore provided a feature called default values. In the definition item for each data template field, you can specify a value for the Default value property. When a user creates an item, Sitecore copies the default value from the field definition item into the corresponding field value in the new item.

Reset Blank

If you select the Reset blank checkbox in a field definition item, after a CMS user sets that field to a blank value, Sitecore reflects the standard value for that field (if the standard values for the data template define a value for the field) or its default value (if the field defines a default value and standard values for the data template to not define a value for the field). In the case of default values, Sitecore actually copies the value, whereas for standard values, a null value in the field cause Sitecore to reflect the standard value for that field.

Resetting Fields to Their Standard Values

If a field has a value that overrides its standard value, users with appropriate access rights can reset that field value to its standard value. If you checked the Reset blank checkbox in a definition item for the field, then clearing the field value in an item based on the data template that contains that field resets that field to its standard value. To reset layout details to their standard value, click the Reset command in the Layout group on the Presentation tab. To reset insert options to their standard value, click the Reset command in the Insert Options group on the Configure tab. To reset any other field, click Reset in the Fields group on the Versions tab, and then select the fields to reset. If the Sitecore standard template defines those fields, then you need to select the Standard Fields checkbox in the View group on the View tab before you click Reset in the Fields group on the Versions tab. Remember to hide the standard fields afterwards.

Template Inheritance

Data templates can inherit fields and standard values from one or more base templates. If template X inherits from templates A and B, then items based on template X reflect the standard values defined by both A and B.

Standard values do not apply layout deltas. For example, if template X inherits from template A, template X will not store a layout delta for comparison against the layout details defined in the standard values of template A. Instead of storing the differences between the layout details in each standard values item, Sitecore copies layout details from the standard values of template A to the standard values of template X before applying the changes only to layout details in X.

Sitecore supports multiple inheritance for data templates (unlike .NET, which supports sequential inheritance in allowing a class to only derive from only one other class). In fact, a data template can inherit from the same base template more than once; template X can inherit from both base templates B and C, where both B and C inherit from base template A. If A, B, and C all define a standard value for a field defined in A, which value applies?

First, I would avoid creating such conditions. Second, I would not count on the logic for determining the standard value in such cases, especially considering complex data inheritance chains. It appears that Sitecore uses the first standard value it finds. For example, if X inherits from A before B, X shows the standard value defined by A; if X inherits from B before A, X shows the standard value defined by B, but again I would avoid such data conditions whenever possible.

Important Standard Values

To minimize administration and maximize consistency, I recommend that, whenever possible, you define the following types of values in standard values for data templates rather than in individual items:

  • Layout Details: In standard values for a data template, specify presentation components and rendering rules to apply to the item for various devices
  • Insert Options: In standard values for a data template, control the types of items that users can insert beneath items associated with that data template
  • Initial Workflow: In standard values for a data template, specifies the default publishing workflow for items of that type

Clones

Cloned items provide the equivalent of standard values for clones. For example, clone N of cloned item M treats values in M as if they were standard values for N.

Standard Values APIs

To determine if the standard values item exists for a data template, you can check for null in the StandardValues property of a Sitecore.Data.Items.TemplateItem object representing the data template (this will return null if the context user does not have read access to the standard values item, but that is an unlikely case if they can access its data template). For example, to determine whether standard values exist for the data template associated with the context item:

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
Sitecore.Diagnostics.Assert.IsNotNull(item, "item");
Sitecore.Data.Items.Item stdVals = item.Template.StandardValues;
bool hasStandardValues = stdVals != null;

You can use the CreateStandardValues() method of the Sitecore.Data.Items.TemplateItem class to create the standard values item:

if (!hasStandardValues)
{
  stdVals = item.Template.CreateStandardValues();
  Sitecore.Diagnostics.Assert.IsNotNull(stdVals, "stdVals");
  hasStandardValues = true;
}

Once you have the standard values item, you can treat it like any other item, such as to update field values (for details, see the Sitecore Content API Cookbook linked at the bottom of this page).

To determine whether a field contains its standard value, you can use the ContainsStandardValue Boolean property exposed by the Sitecore.Data.Fields.Field class (or any class that inherits from the Sitecore.Data.Fields.Field, such as Sitecore.Data.Fields.MultlistField). For example, do determine if the field named Text in the context item contains its standard value:

Sitecore.Data.Fields.Field text = item.Fields["text"];
bool containsStandardValue = text != null && hasStandardValues && text.ContainsStandardValue();

Conclusion

If you have any additional information about standard values, please comment on this blog post.

Resources