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;
            try
            {
                if (item != null && item.Version.Number == 1)
                {
                    var fallbackItem = item.GetFallbackItem();
 
                    item.Editing.BeginEdit();
                    fallbackItem.Fields.ReadAll();
 
                    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);
                        }
                    }
 
                    item.Editing.EndEdit();
                    item.Editing.AcceptChanges();
                }
            }
            catch (Exception ex)
            {
 
                if (item != null)
                {
                    item.Editing.CancelEdit();
                }
            }
        }
    }
}


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


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

That is all.

Example:

1. Select "Fallback version" for Portuguese.






















2. "Add a new version"


























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







Done! Happy Sitecoring!



Reference:

https://doc.sitecore.net/sitecore_experience_platform/setting_up_and_maintaining/language_fallback/setting_up/enable_and_set_up_language_fallback

http://geekswithblogs.net/amaniar/archive/2011/12/12/using-sitecores-pipeline-to-pre-populate-item-in-current-language.aspx



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...