6

Our customer had a request for possibility to copy or duplicate an item without a previous language versions. Only the latest versions have to be created.

Check my solution on http://sitecore-masters.com/en/2016/11/28/duplicate-item-without-previous-versions. Do you know any other solution?

3

Here is an another example. What I am not happy that reflection and message collection are being used. But it can be as workaround for quick solution.

Configuration:

  <sitecore>
  <commands>
      <command name="custom:CopyLatestVersionTo" type="Sitecore.CopyTo.CopyLatestVersionTo, Sitecore.CopyTo" />
  </commands>
  <processors>
      <uiCopyItems>
          <processor mode="on" type="Sitecore.CopyTo.Pipelines.CopyLatestTo, Sitecore.CopyTo" method="OnlyLatestVersion" patch:after="processor[@type='Sitecore.Shell.Framework.Pipelines.CopyItems,Sitecore.Kernel'][@method='Execute']" />
      </uiCopyItems>
  </processors>

Command:

[Serializable]
public class CopyLatestVersionTo : Sitecore.Shell.Framework.Commands.CopyTo
{
    public override void Execute(CommandContext context)
    {
        var args = new CopyItemsArgs();
        var instance = Activator.CreateInstance(typeof(Sitecore.Shell.Framework.Items));
        var methodInfos = new List<MethodInfo>(instance.GetType().GetMethods(BindingFlags.Static | BindingFlags.NonPublic));
        var method = methodInfos.FirstOrDefault(m => m.Name == "Start" && m.GetParameters().Length == 4);
        args.AddMessage("command:copylatest");
        method.Invoke(instance, new object[] {"uiCopyItems", args, context.Items[0].Database, context.Items});
    }
}

Processor:

 public class CopyLatestTo
  {
    protected virtual void OnlyLatestVersion(CopyItemsArgs args)
    {
      var pipelineMessages = args.GetMessages(PipelineMessageFilter.Informations);
      if (pipelineMessages != null && pipelineMessages.Any(m=>m.Text == "command:copylatest"))
      {
        var copies = args.Copies;
        foreach (var copy in copies)
        {
          var currentVersion = copy.Versions.GetLatestVersion();
          foreach (var itemVersion in copy.Versions.GetVersions(true))
          {
            if (!itemVersion.Version.Number.Equals(currentVersion.Version.Number))
            {
              itemVersion.Versions.RemoveVersion();
            }
          }
        }
      }
    }
  }

Menu Item: "/sitecore/content/Applications/Content Editor/Context Menues/Default/Copying/Copy Latest To"

enter image description here

enter image description here

Result: enter image description here

1
  • Indeed the efficient solution for this kind of requirement. Simply worked. Thanks!
    – Dash
    Nov 13 '19 at 8:08
1

You can use for your requirement this module:

https://marketplace.sitecore.net/Modules/S/Smart_Tools_Add_Version_and_Copy_Content.aspx?sc_lang=en

a. It adds a new version to the selected destination language version. If you have 3 versions it's adding just the last one.

b. It copies the content from the source language (context item) to all selected destination language.

c. It has the option of including the child items as well.

http://www.nehemiahj.com/2013/10/smart-tools-1-add-version-and-copy.html

3
  • Hi Climber, your comment is good, but it does not solve my problem. This tool copies only versions inside item. It does not create new item. Nov 30 '16 at 9:34
  • I added you a solution for one of the cases, you can download the module and decompile the bin files and check how it was made. You can extend it for the second case Nov 30 '16 at 11:59
  • OK, thanx for it. Nov 30 '16 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.