14 February 2018

Copy item values to new language version in Sitecore

There is a requirement in multi-lingual sitecore setup, when we create a new non default language version for an item, the content editor keen to have the functionality to copy over all the fields values to save them some time from manually setup all again for each language.

(The code has been tested against Sitecore 8.2 and I cannot see why it does not work for later version.)

First, you need to setup Sitecore multi Lingual properly. reference to link

Then code sample:

using Sitecore.Data.Items;
using Sitecore.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data.LanguageFallback;
namespace Test.Feature.Language.Pipelines
    public class CopyEnglishContent
        public void OnVersionAdded(object sender, EventArgs args)
            var item = Event.ExtractParameter(args, 0) as Item;
                if (item != null && item.Version.Number == 1)
                    var fallbackItem = item.GetFallbackItem();
                    foreach (Sitecore.Data.Fields.Field field in fallbackItem.Fields)
                        if ((!field.Shared && !field.Name.StartsWith("__") && field.Name.Trim() != "") || field.Name == "__Final Renderings")
                            item.Fields[field.Name].SetValue(field.Value, true);
            catch (Exception ex)
                if (item != null)

Apart from above, we just need to put it in the right spot for configuration purpose.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <event name="item:versionAdded">
        <patch:delete />
      <event name="item:versionAdded">
        <handler type="Test.Feature.Language.Pipelines.CopyEnglishContent, Test.Feature.Language" method="OnVersionAdded" />

That is all.


1. Select "Fallback version" for Portuguese.

2. "Add a new version"

3. A new version have been created for "Portuguese"

Done! Happy Sitecoring!




No comments:

Post a Comment

WFFM Web form for marketer fail to submit in Sitecore

I have setup a standard form using WFFM in Sitecore 8.2. It always fail when submit. Error as below, Error: Exception: System.Data.DataE...