Reset a field to its Standard Value

Version: 10.4

You can use the Sitecore.Data.Fields.Field.Reset method to reset a field to its standard value. For example, to reset the value of the Title field in the /Sitecore/Content/Home item in the Master database to its standard value:

RequestResponse
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"];

home.Editing.BeginEdit();

titleField.Reset();

home.Editing.EndEdit();

Updating the value of a Sitecore.Data.Fields.Field to an empty string or the standard value of the field does not cause the value of the field to revert to the value defined in the standard values item associated with its data template. Use the Sitecore.Data.Fields.Field.Reset method to reset a field to its standard value.

When you reset a field to its standard value, Sitecore does not expand tokens such as $name in the standard value. You can use a master variable replacer to replace tokens.

RequestResponse
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"];

home.Editing.BeginEdit();

titleField.Reset();

home.Editing.EndEdit();

home.Editing.BeginEdit();

Sitecore.Data.MasterVariablesReplacer replacer =

Sitecore.Configuration.Factory.GetMasterVariablesReplacer();

replacer.ReplaceField(home,titleField);

home.Editing.EndEdit();
Note

You must commit the reset operation before using the master variable replacer on the field value.

Do you have some feedback for us?

If you have suggestions for improving this article,