0

I wrote a custom item:created event handler that creates an item of template B whenever an item of template A is created. It works well, except for the cases when the parent folder is being duplicated—then I don't want a new B item created, as there will be a duplicated B item anyway.

In my item:created handler, is it possible to find out whether the item is being created "from scratch", or if it's being copied/duplicated from an existing item?

  • 1
    There is really no help in the ItemCreatedEvent args, and in the "item:creating" event, you only get the parent element as a small bonus. But unless you have a way, to check if the parent item is a duplicate (prehaps check the created time on it?), there isent a lot of help – Sandbeck Jan 20 '17 at 12:20
  • @Sandbeck Checking the created date was a great idea... but I just verified and it's not set to "now" when duplicating. It's just taken straight from the original item, along with the updated date. – Dmytro Shevchenko Jan 20 '17 at 12:31
  • 1
    Just as an off-hand note, other than checking for copy/duplicate you should also look for packages being installed. You can use Sitecore.ContentSearch.Maintenance.IndexCustodian.IsIndexingPaused("indexName") (maybe use the master index?) to look for an indication that a mass update is happening. – Jason St-Cyr Jan 20 '17 at 13:29
  • 2
    Asking the obvious perhaps; but why not use a Branch Template? – Mark Cassidy Jan 20 '17 at 15:16
  • 1
    Check out blog.coria.com/… for the solution to Created and Updated date not being set to "now" when duplicating. If that can help you. – Dalibor Kovač Jan 20 '17 at 20:32
1

I have checked through the decompiled source and apparently those events are quite decoupled from each other.

For your particular case I would recommend you to introduce some kind of a flag field (e.g. IsCopy) on the template and use the item:copying/copied event to set that flag to true on the copy item. After that you can easily check that field value in the item:created event.

The solution is quite dirty, but to me it sounds like the easiest way of implementing the requirement.

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.