Editors often create new items using the duplication of an existing item or they want to maintain the content of some fields of an item. The duplication of an item causes the duplication of all versions of the item for all languages. It deals with creating a 1:1 copy. In the best case scenario, the editors delete all unnecessary versions in retrospect in the newly created item. This is not a very user friendly solution.

For this reason, I have created two new commands that will perform the duplication of the item but on the last version of the item. This will either be performed in all language versions (command: “Duplicate All Language Versions“) or only for only for one language version, the one in which the editor is currently in (command: “Duplicate Current Language Version“).
Now, I will describe the steps that need to be performed.

Expand the context menu and ribbon in the “Core” database by two new commands:

  1. Run “Desktop“ and switch to the “Core“ database using the icon in the lower right hand corner.
  2. Run “Content Editor” via the “Start“ icon.
  3. Go to the item “/sitecore/content/Applications/Content Editor/Menues/Duplicate” Here, set up two new items from the file: “/sitecore/templates/System/Menus/Menu item”.
    1. Duplicate All Language Versions – displayName = “Duplicate All Language Versions”, message = “custom:duplicateallversions”
    2. Duplicate Current Language Version – displayName = “Duplicate Current Language Version”, message = “custom:duplicatecurrentversion”
  4. Set up two items for the context menu in the same way “/sitecore/content/Applications/Content Editor/Context Menues/Default”.

Now, you have to create classes that execute the commands.

using System.Collections.Specialized;
using Sitecore;
using Sitecore.Shell.Framework.Commands;
namespace MyNamespace
{
   public class DuplicateItemAllLanguages : Duplicate
   {
      public override void Execute(CommandContext context)
      {
         if (context.Items.Length != 1 || context.Items[0] == null) return;
         var item = context.Items[0];
         var parameters = new NameValueCollection();
         parameters.Add("database", item.Database.Name);
         parameters.Add("id", item.ID.ToString());
         parameters.Add("language", item.Language.ToString());
         parameters.Add("version", item.Version.ToString());
         parameters.Add("latestversiontype", "all");
         Context.ClientPage.Start("uiDuplicateItem", parameters);
      }
   }
}

and the second class

using System.Collections.Specialized;
using Sitecore;
using Sitecore.Shell.Framework.Commands;
namespace MyNamespace
{
   public class DuplicateItemCurrentLanguage: Duplicate
   {
      public override void Execute(CommandContext context)
      {
         if (context.Items.Length != 1 || context.Items[0] == null) return;
         var item = context.Items[0];
         var parameters = new NameValueCollection();
         parameters.Add("database", item.Database.Name);
         parameters.Add("id", item.ID.ToString());
         parameters.Add("language", item.Language.ToString());
         parameters.Add("version", item.Version.ToString());
         parameters.Add("latestversiontype", "current");
         Context.ClientPage.Start("uiDuplicateItem", parameters);
      }
   }
}

Adding new commands to the file: “\Website\App_Config\Commands.config” the section “Commands”:

<commands>
   ...
   <command name="cutsom:duplicateallversions" type="MyNamespace.DuplicateItemAllLanguages, MyAssembly" />
   <command name="custom:duplicatecurrentversion" type="MyNamespace.DuplicateItemCurrentLanguage, MyAssembly" />
</commands>

Now you have to overload the processor that performs the duplication of the item. Therefore, we will create a new class of processor, into which we will copy the “Execute” method from the “Sitecore.Shell.Framework.Pipelines.DuplicateItem” class.

namespace MyNamespace
{
   public class UIDuplicateItem
   {
       public void Execute(ClientPipelineArgs args)
      {
         ...
         /* Original: Context.Workflow.DuplicateItem(obj, args.Parameters["name"]); */
         Item ims = Context.Workflow.DuplicateItem(item, args.Parameters["name"]);
         if (args.Parameters["latestversiontype"] != null)
         {
           var latestOnlyInCurrent = args.Parameters["latestversiontype"].Equals("current", StringComparison.InvariantCultureIgnoreCase);
           var versions = ims.Versions.GetVersions(true);
           var currentLanguageName = args.Parameters["language"];
           foreach (var version in versions)
           {
              if (! version.Versions.IsLatestVersion() || (latestOnlyInCurrent && ! version.Language.Name.Equals(currentLanguageName, StringComparison.InvariantCultureIgnoreCase)))
              {
                 version.Versions.RemoveVersion();
              }
           }
        }
      }
   }
}

Register the processor in the Processors section of the “Web.config” the section “Processors”:

      <uiDuplicateItem>
        <processor mode="on" type="Sitecore.Shell.Framework.Pipelines.DuplicateItem,Sitecore.Kernel" method="CheckPermissions" />
        <processor mode="on" type="Sitecore.Shell.Framework.Pipelines.DuplicateItem,Sitecore.Kernel" method="GetName" />
        <!-- <processor mode="on" type="Sitecore.Shell.Framework.Pipelines.DuplicateItem,Sitecore.Kernel" method="Execute" /> -->
        <processor mode="on" type="MyNamespace.DuplicateItem, MyAssembly" method="Execute" />
      </uiDuplicateItem>

If we are using Buckets, then it is necessary to copy the “Execute” method from the “Sitecore.Buckets.Pipelines.UI.ItemDuplicate” class in the “Sitecore.Buckets.dll” library and turn off the related processor supplying the configuration of the buckets.