Sitecore remove previous versions on duplicating or copying item

Hi all,

When duplicating or copying an item in Sitecore, all the versions are copied to the new item as well. Sometimes this is unwanted behaviour, especially when the first version of the item was not part of the workflow. When you publish the item, the latest published state would be published (i.e. the non-workflow version)

A simple trick is to remove the previous versions of the item when duplicating or copying item. This is done by intercepting the uiCopyItems and uiDuplicateItem.
Note that for uiDuplicateItem, the Sitecore.Buckets.config aborts the pipeline before the original duplicate pipeline. To avoid this, we make sure our custom pipeline is triggered first before the bucket config.

Custom config patch:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
<processors>
  <uicopyitems>
    <processor mode="on" type="Sitecore.Shell.Framework.Pipelines.CopyItems,Sitecore.Kernel" method="Execute">
      <patch:attribute name="type">Assembly.CopyItem, Assembly</patch:attribute>
      <patch:attribute name="method">Execute</patch:attribute>
    </processor>
  </uiCopyItems>
  <uiduplicateitem>
    <processor mode="on" type="Sitecore.Shell.Framework.Pipelines.DuplicateItem,Sitecore.Kernel" method="Execute">
      <patch:attribute name="type">Assembly.DuplicateItem, Assembly</patch:attribute>
      <patch:attribute name="method">Execute</patch:attribute>
    </processor>
  </uiDuplicateItem>
</processors>
Sitecore.buckets.config:
1
2
3
<uiduplicateitem>
  <processor mode="on" patch:after="*[@method='Execute']" type="Sitecore.Buckets.Pipelines.UI.ItemDuplicate, Sitecore.Buckets"    method="Execute"/>
</uiDuplicateItem>
Then we have a Sitecore extension helper to remove the previous versions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static void RemovePreviousVersions(this Item myItem, bool includeAllLanguages)
{
    using (new Sitecore.SecurityModel.SecurityDisabler())
    {
        // get the most recent version
        Item currentVersion = myItem.Versions.GetLatestVersion();
        Item[] versions = myItem.Versions.GetVersions(includeAllLanguages);
 
        // loop through the item versions
        foreach (Item itemVersion in versions)
        {
            // remove the version if it is not the most recent
            if (!itemVersion.Version.Number.Equals(currentVersion.Version.Number))
            {
                itemVersion.Versions.RemoveVersion();
            }
        }
    }
}

DuplicateItem class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class DuplicateItem
    {
        private Item _itemToDuplicate;
 
        public new void Execute(ClientPipelineArgs args)
        {
            Item copy = Duplicate(args);
            if (copy == null)
                return;
 
            if (_itemToDuplicate == null)
                return;
        }
 
        private Item Duplicate(ClientPipelineArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            Item item = GetItemToDuplicate(args);
            _itemToDuplicate = item;
            if (item == null)
            {
                SheerResponse.Alert("Item not found.", new string[0]);
                args.AbortPipeline();
            }
            else
            {
                Item parent = item.Parent;
                Item bucketItemOrSiteRoot = item.GetParentBucketItemOrSiteRoot();
                if (BucketManager.IsBucket(bucketItemOrSiteRoot) && BucketManager.IsBucketable(item))
                {
                    return Context.Workflow.DuplicateItem(item, args.Parameters["name"]);
                }
                if (parent == null)
                {
                    SheerResponse.Alert("Cannot duplicate the root item.", new string[0]);
                    args.AbortPipeline();
                }
                else if (parent.Access.CanCreate())
                {
                    Log.Audit(this, "Duplicate item: {0}", new string[] { AuditFormatter.FormatItem(item) });
                    var duplicatedItem = Context.Workflow.DuplicateItem(item, args.Parameters["name"]);
                    duplicatedItem.RemovePreviousVersions(true);
                    args.AbortPipeline();
                }
                else
                {
                    SheerResponse.Alert(Translate.Text("You do not have permission to duplicate \"{0}\".", new object[] { item.DisplayName }), new string[0]);
                    args.AbortPipeline();
                }
            }
            return null;
        }
 
        private Item GetItemToDuplicate(ClientPipelineArgs args)
        {
            Language language;
            Database database = Factory.GetDatabase(args.Parameters["database"]);
            Assert.IsNotNull(database, args.Parameters["database"]);
            string str = args.Parameters["id"];
            if (!Language.TryParse(args.Parameters["language"], out language))
            {
                language = Context.Language;
            }
            return database.GetItem(ID.Parse(str), language);
        }
    }

CopyItem class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class CopyItem : Sitecore.Shell.Framework.Pipelines.CopyItems
{
    public virtual void Execute(CopyItemsArgs args)
    {
        Assert.ArgumentNotNull((object)args, "args");
        Item target = CopyItems.GetDatabase(args).GetItem(args.Parameters["destination"]);
        Assert.IsNotNull((object)target, args.Parameters["destination"]);
        ArrayList arrayList = new ArrayList();
        foreach (Item itemToCopy in CopyItems.GetItems(args))
        {
            if (itemToCopy != null)
            {
                var copiedItem = this.CopyItem(target, itemToCopy);
                copiedItem.RemovePreviousVersions(true);
                arrayList.Add((object) copiedItem);
            }
        }
        args.Copies = arrayList.ToArray(typeof(Item)) as Item[];
    }
}

And that's it. Note that I applied this to Sitecore 7.2 Update 5

HTH,
Andreas

Comments

Popular posts from this blog

SharePoint 2013 anonymous access add attachments to list item

CRM Plugin - Parent and Child Pipeline