Sitecore 5.2: Copy from one language to another

This piece of code moves all fields from one language to another. It is a sample code, so it is hardcoded to move from danish to swedish. Change the code to use your languages before using it:

void Switch(Sitecore.Data.Items.Item item)
{
  Sitecore.Data.Items.Item da = Sitecore.Context.ContentDatabase.Items[item.ID, Sitecore.Globalization.Language.Predefined.Danish];
  Sitecore.Data.Items.Item se = Sitecore.Context.ContentDatabase.Items[item.ID, Sitecore.Globalization.Language.Predefined.Swedish];
  se.Editing.BeginEdit();
  da.Editing.BeginEdit();
  foreach (Sitecore.Data.Fields.Field field in da.Fields)
  {
    if ( !field.Shared && !field.Name.StartsWith( "__" ) && field.Name.Trim() != "" )
    {
      se.Fields[field.Name].Value = field.Value;
      field.Value = "";
    }
  }
  da.Editing.EndEdit();
  se.Editing.EndEdit();
}
About these ads

About Brian Pedersen

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 5 and tagged , , . Bookmark the permalink.

2 Responses to Sitecore 5.2: Copy from one language to another

  1. Lars Nielsen says:

    Hi Brian,

    Good to see some movement on this blog. And good snippet idaa

  2. Sander says:

    Hi Brian,

    Great idea, it doesn’t seem to work for me however. In short, not every item had a version for the ‘new’ language so I created one. Still, changes made in either the source or the destination language item don’t show up. Any help would be appreciated.

    Sander Falise.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s