Tapping Duplicate Item Pipeline
One of the latest requirements and challenging request was to tap on to duplicate item pipeline on latest version of Sitecore. As of this writing, Sitecore 8.1, update 3.
Every time I have a problem on my hand, to come up with a solution, I clearly imprint on my brain what my goal is, this actually helps find different ways to achieve what you want the system on hand to do.
My goal was to tap on to duplicateitem pipeline somehow to do few renaming and change of few references post duplication of a specific item on the content tree. The key here is specific item, I dont want this custom code to run if the duplication is done on any other item, so, it is important to know where and how I can patch this to successfully instruct sitecore. “Hey! If content author is duplicating this ‘xyz’ item only, perform a set of operations”
I ran across couple blogs to start understanding what happens behind the scenes. Not much of help as either these are outdated and probably not close to what I was looking for….
So, time to write it up, so any one who is looking for some direction on how to tap to duplicate pipeline.
Trials and Failure
I went ahead and looked at my “showconfig.aspx”, found the below related config section under processors and this felt like perfect place I could patch on to –
Based on this article, I patched on to before the buckets related pipeline for duplicate
that is ‘Sitecore.Buckets.Pipelines.UI.ItemDuplicate’
The challenge here was the client args in the info object had array of fields under the item that is to be duplicated which I had no way to overwrite. So, it does not achieve my goal, scrap that.
Then, I thought I will tap SaveItem pipeline, but, I will loose the context of which item is being duplicated and either way’s duplicateitem does not trigger save item pipeline, so that would not work.
I am hitting walls in every direction, but, I need to keep looking and trying…
I was curious what does Sitecore.Buckets Item Duplicate pipeline does with Client Args as that route made most sense. It seems only thing the code uses from Client Args was the “Name” for the new item.
While exploring the code on Kernel dll using reflector, I see that it directly calls Context.Workflow.DuplicateItem(item, args[“name”)). This is getting interesting, seems like I see light at the end of the tunnel.
Explored more deeper, chain of methods called from this point one ->
On derived type of copyItem of Provider, I see something that caught my eye.
It calls GetDataEngine and proceeding with CopyItem on DataEngine. Dug more deeper..
It calls CopyItemCommand’s Execute, Voila!!! I found my answer. Panicking with excitement, I tried extending CopyItemCommand’s prototype using the below kind of config edits
<database id=”master” singleInstance=”true” type=”Sitecore.Data.Database, Sitecore.Kernel”>
<obj type=”Yournamespace.ClassName, DLL” />
I followed similar syntax from Sitecore.Buckets.Config to make this work. There is a catch, always make sure you are overriding CreateInstance method and returning your new class in that method, otherwise it will not consider your class as valid prototype and will take the default one.
Now extend the class “Sitecore.Data.Engines.DataCommands.CopyItemCommand”
I could get the source item, destination item(parent), copy ID(new id to be created) and I can extend the Executed method now to add my own business logic.
Make sure you you call base.Executed() to ensure the base functionality is still in place.
Good Reads/Helpful Related articles