Clear fields when copying Sitecore items

On a solution we developed recently we had some fields with individual item settings that we needed to clear when the items was duplicated. To me this seemed like a task for Sitecore eventhandlers.

Eventhandlers are really easy to use and with the possibility in Sitecore to use multiple config-files it gets even easier.

I created a config file named ClearFields.config and placed it in /App_Config/Include-folder.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <events>
            <event name="item:copied">
                <handler type="Sitecore.ClearFields, Sitecore" method="OnItemCopied" />
            </event>
        </events>
    </sitecore>
</configuration>

Basically what it does is to tell Sitecore that when it copies an item and fires the item:copied event, the OnItemCopied method should be invoked from my custom class. Here are some examples and the list of arguments that can be used for events.

Next thing up would be to create some code.

using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore.Data.Items;
using Sitecore.Events;

namespace Sitecore
{
    public class ClearFields
    {
        protected void OnItemCopied(object sender, EventArgs args)
        {
            if (args != null)
            {
                Item item = Event.ExtractParameter(args, 1) as Item;
                if (item == null)
                    return;

                if (!string.IsNullOrEmpty(item["My field To Clear"]))
                {
                    item.Editing.BeginEdit();
                    item.Fields["My field To Clear"].Value = "";
                    item.Editing.EndEdit();
                }
            }
        }
    }
}

As you see the above code is quite easy. I get the newly created item from the event parameters, check to see that the field actually exists, sets the item in editing mode and then clears the field.

Note: In order to get the newly created item instead of the
original item, use parameter index 1 instead of 0.

Now I don’t like hard-coded values and neither should you. Fortunately Sitecore has made it easy for us developers to put settings in our config-files. Below is my slightly updated config.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <settings>
            <setting name="ClearFields.FieldsToClear" value="header|body" />
        </settings>
        <events>
            <event name="item:copied">
                <handler type="Sitecore.ClearFields, Sitecore" method="OnItemCopied" />
            </event>
        </events>
    </sitecore>
</configuration>

As you see I have added a settings section and a setting with a pipe-separated value indicating the fields to be cleared.

My updated code now looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore.Data.Items;
using Sitecore.Events;

namespace Sitecore
{
    public class ClearFields
    {
        protected void OnItemCopied(object sender, EventArgs args)
        {
            if (args != null)
            {
                Item item = Event.ExtractParameter(args, 1) as Item;
                if (item == null)
                    return;
                foreach (var field in GetFieldsToClear())
                {
                    if (!string.IsNullOrEmpty(item[field]))
                    {
                        item.Editing.BeginEdit();
                        item.Fields[field].Value = "";
                        item.Editing.EndEdit();
                    }
                }
            }
        }

        private static List<string> GetFieldsToClear()
        {
            string strFields = Configuration.Settings.GetSetting("ClearFields.FieldsToClear");
            return strFields.Split('|').Select(strField => strField.Trim()).ToList();
        }
    }
}

Now the function iterates over the pipe-separated field names and clears them on the item copied or duplicated. And yes, item.Field[] supports Sitecore field IDs so you don’t need to use field names.

That’s it!

Note: Eventhandlers are powerful but could impact Sitecore
performance. The above code will execute everytime someone copies
an item in Sitecore.

Update: Remember that you need to change the values on each language if your fields are not shared.

Share

One thought on “Clear fields when copying Sitecore items

  1. Pingback: Tweets that mention Clear fields when copying Sitecore items | Larres CMS ramblings -- Topsy.com

Leave a Reply

Your email address will not be published. Required fields are marked *