Sitecore tips and tricks and community news !

Post Top Ad

Post Top Ad

Monday, October 16, 2017

Item Created event, how to use properly ?



Story: 

During work with EXM I had very interesting story last time. I've installed EXM in correct way, according to guide standards. I was happy that any problems didn't happen and then I realized I have one :)

During EXM tests I've created One column Message, firstly new item was created (cool!). But after that I saw new item has one issue, message tab can't load.

 

Later I've figured out that it has empty Body field, although branch template has definition for that by default, so that what probably the issue of incomplete loading the message tab.


Since that point I've started looking for problem which cases breaking of creation an email in EXM. Because email message creation is based on branch, it's connected with branching I assumed. After a lot of searching I've noticed that when I comment out my "item:created" event declaration, EXM issue disappears. I've found it already ! I thought :)

I was digging more and what I saw was really interesting. mentioned "item:created" event has issue on method "DisplayNameSetup", because I had method like that.

Conclusions? Well, I can't setup the display name on item created because it will broke branching engines ! (important, not only EXM branches, but all of them) Yey ! But still, why ? :) Because that question was still in my head I've talked to Sitecore support and what is established is below.

Solution:

Firstly we have to establish one thing, maybe it's not obvious, but "item:created" event doesn't say that process of creation has been finished. Even more, there is at least 4 event handlers that occurs after our item created event. According to my issue with EXM, it consist also method which sets 'body' to item.

After each mentioned event handler there is also saving handler, which occurs when item will be changed during creation process. Hence, when we are changing item during creation, it blocks next event handlers to be called.

If you want to change items, it's better to use:
 item:versionAdded and item:versionAddedRemote events.
That events occurs after item has been created, so we have sure we don't interrupt process of creation and filling item fields.

Explanation:

Accessing an item from the ItemProvider in the CreateItem() method or in the 'item:created' event handler has no versions. The version is added later inside the Sitecore.Nexus assembly command.

When the version has been added, the Sitecore.Nexus command also copies field values from the branch template to the item and only after that the item gets saved. So mentioned action SaveItem() of the ItemProvider ensures that the item has at least 1 version and other case it will add one.

Our problem form the story starts when an item is edited inside the CreateItem() method of the ItemProvider or in the 'item:created' event.
Let's go through the path. After editing our item.Editing.EndEdit() method saves the item, but it doesn't have any version, hence version will be added in SaveItem().

Afterwards, when we go to the Sitecore.Nexus command some of the branch template field values will not be copied to the new item, because it already has a version (from SaveItem()).
In our story, result will be that 'body' field of the branch template will be not added to the item.

Hope it will help some of you :)
 

No comments:

Post a Comment