Copying English version content to new language versions – Sitecore

While working on our latest Sitecore project, our client requested that all language versions to be based on the current English version which means copying the current English version fields’ content. Some fields or all fields (depending on the page) are then translated to the corresponding language version manually.

By default, when creating a new language version Sitecore resets to the page template’s Standard Value’s content state. This means marketing people will have to refill the data again for lots of cases in which many fields need to have the English content.

In addition to the page item’s fields, we have to migrate the content of all the datasource items too. Our content structure uses datasources that are located under ‘localcomponents’ folder underneath the page item itself. Our datasource items also have children items, such as a carousel datasource item with sub-items as slides. So we when are copying content we have to copy the content of all items underneath the ‘localcomponents’ folder for the page item.

Code

The following code will be executed on the VersionAdded event:

 <event name="item:versionAdded">
    <handler type="namespace.VersionAddedEvent, assemblyName" method="Process" />
 </event>
public class VersionAddedEvent
 {
   public void Process(object sender, EventArgs e)
   {
      try
      {
      Item newItem = Event.ExtractParameter(e, 0) as Item;

      if (newItem == null)
      return;

      //Only proceed if master DB
      var masterDB = newItem.Database;
      if (masterDB == null || masterDB.Name.ToLower() != "master")
      return;

      //This check makes sure not to copy content from the english version when creating a      new version of an existing language version e.g. version 2 of DE version
      if (newItem.Versions.Count > 1)
      return;

      //Get the english version of the item, since versions are based on the english version
      Sitecore.Globalization.Language enLanguage = Sitecore.Data.Managers.LanguageManager.GetLanguage("en");
      Sitecore.Data.Items.Item englishVersion = masterDB.GetItem(newItem.ID, enLanguage);

      if (englishVersion == null)
      return;

      ////Copy values from enlish version to the new language version
      //load a fields
      englishVersion.Fields.ReadAll();
      newItem.Editing.BeginEdit();
      foreach (Field field in englishVersion.Fields)
      {
         //Make sure to get content from used fields and non-sitecore fields (usually start with      double underscore)
         if (field != null && !string.IsNullOrEmpty(englishVersion[field.Key]) &&      field.Key.StartsWith("__") == false)
         {
            newItem[field.Key] = englishVersion[field.Key];
         }
      }
      newItem.Editing.EndEdit();

      //Get the page's localcomponent folder (if exists)
      var localcomponentsRef = englishVersion.Children.SingleOrDefault(w =>      w.IsDerived(Constants.LocalComponents.ID) && w.Name.ToLower() ==      "localcomponents");

      if (localcomponentsRef == null)
      return;

      //Create a new version of the localcomponents folder in the new language
      var localcomponents = masterDB.GetItem(localcomponentsRef.ID, newItem.Language);
      if (localcomponents.Versions.Count == 0)
      {
         localcomponents.Editing.BeginEdit();
         localcomponents.Versions.AddVersion();
         localcomponents.Editing.EndEdit();
      }

      ////Start creating versions of all datasource items underneath the 'localcomponents'      and moving content to the new lang version
      foreach (var itemRef in localcomponentsRef.Axes.GetDescendants())
       {
         var item = masterDB.GetItem(itemRef.ID, newItem.Language);
         if (item.Versions.Count > 0)
            continue;

         var newVersion = item.Versions.AddVersion();

         itemRef.Fields.ReadAll();
         item.Editing.BeginEdit();
         foreach (Field field in itemRef.Fields)
         {
            if (field != null && !string.IsNullOrEmpty(itemRef[field.Key]) &&      field.Key.StartsWith("__") == false)
            {
               item[field.Key] = itemRef[field.Key];
            }
         }

         item.Editing.EndEdit();
       }
      }
      catch (Exception ex)
      {
         Sitecore.Diagnostics.Log.Error(ex.ToString(), this);
      }
    }
 }

So what happens when I delete the language version and add it again?

I also have a ‘Version Removed’ event handler to remove the datasources’ item versions when removing the language version of the page item:

 <event name="item:versionRemoved">
   <handler type="namespace.VersionRemovedEvent, assemblyName" method="Process" />
 </event>
public class VersionRemovedEvent
{
 public void Process(object sender, EventArgs e)
  {
  try
  {
   Item deletedItem = Event.ExtractParameter(e, 0) as Item;

   if (deletedItem == null)
    return;

   var masterDB = deletedItem.Database;
   if (masterDB == null || masterDB.Name != "master")
    return;

   //Get the localcomponents folder item
   var localcomponentsRef = deletedItem.Children.SingleOrDefault(w => w.Name.ToLower() == "localcomponents");

   if (localcomponentsRef == null)
    return;

   //remove the localcomponent version
   localcomponentsRef.Versions.RemoveVersion();
   //remove all the versions of the datasource items underneath the localcomponents
   foreach (var itemRef in localcomponentsRef.Axes.GetDescendants())
   {
     itemRef.Versions.RemoveVersion();
   }
  }
  catch (Exception ex)
  {
    Sitecore.Diagnostics.Log.Error(ex.ToString(), this);
  }
 }
}

 

I hope this helps and keep in mind that this behavior is usually not correct. It depends on the business. Our client requested such behavior which may not apply to most other clients.

Advertisements

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 )

w

Connecting to %s