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 🙂

Advertisements

3 thoughts on “Resetting Standard Fields for all Items of a given template

  1. Hey, that’s pretty cool. Thanks for sharing. If you happen to be using the Sitecore PowerShell Extensions module found here http://goo.gl/G2ULkI you can try something like this. The following example will reset the Text field on the home item:

    PS master:\> Get-Item master:\content\home | Clear-ItemProperty -Name Text

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s