Wednesday, July 30, 2014

Sitecore Saving vs Saved Event and when to use which


One of the developers I am mentoring asked me a good question and I thought I would share the answer with the guild in case you are wondering the same.

The question is related to saving items to an external DB when they are saved in Sitecore.  There are really two ways to handle it.

1) Write an Item Provider and store them in a single location.
2) Use the item save events but which one is the right one to use.

In our case it was more beneficial to use option 2.  Using Saved vs Saving depends on whether the external DB will store any data that will be calculated based on saved data rather than simply saving a copy.  Saved happens after the Sitecore item is saved where as Saving will allow you to perform additional operations on the Sitecore item prior to save.

The other thing to be aware of is that when an item is duplicated it will NOT call the saving or saved events unless the user explicitly hits save.  If you require duplicate items to make it to your database too then you will need to implement this event too.

John West wrote a really good article with sample code as well, called Intercepting Item Updates with Sitecore.

If you have any tips, tricks or resources you would like to share with the guild please email them to chris.williams@readwatchcreate.com and we will post them here.