Resetting Standard Fields for all Items of a given template

A very useful feature in Sitecore is the ability to reset fields on an item to their value as defined in Standard Values. This is great, but what if you want to reset fields on many items of the same template?

There doesn’t (yet) appear to be a feature in Sitecore to do this, so I wrote a simple function that performs this task.

Basically, you will need to perform a search to get all your items first, then pass in your list of items and a list of the field names you wish to reset on each item.


public void ResetFields(List<Item> items, List<string> fields)
{
    if (items.Any() && fields.Any())
    {
        foreach (Item item in items)
        {
           ResetFieldsOnItem(item, fields);
        }
    }
}

public void ResetFieldsOnItem(Item item, List<string> fields)
{
    if (item != null && fields.Any())
    {
        using (new Sitecore.Security.Accounts.UserSwitcher(ELEVATED_USER_ACCOUNT, true))
        {
            item.Editing.BeginEdit();

            foreach (string field in fields)
            {
                item.Fields[field].Reset();
            }

            item.Editing.EndEdit();
        }
    }
}

I might create a SPEAK app so that when you click on a template, a button will appear to provide this functionality in the Ribbon – That would be a nice addition 🙂