How to extend the Save event for a Page Type?

Lance Keay asked on September 9, 2015 19:30

Is there a way to extend the save event for a Page Type?

I understand how the global events work, where you can create new EventHandlers for such actions as: ObjectEvents.Update.After and DocumentEvents.Update.After - I'm not looking for this. I don't want to perform an action AFTER the Update, I want to perform the action during the Update, prior to the Document being saved. Maybe there's some clever Kentico function override that I don't know about to do this.

Here's the problem: I have a Company PageType - when it's saved, I need to look at various properties and relationships (such as how many users belong to the company) and then update the Company's properties. If I try to perform this action during the DocumentEvents.Update.After.. well when I save it, it triggers another Update Event on the same Company (since I've just updated some properties)! This can get very recursive very quickly.

I've seen the DocumentEvents.Update.OneTime property, but if I set it to true, they Event is still called multiple times.

Recent Answers

Brenden Kehren answered on September 9, 2015 19:47

I'd suggest performing your global event in the DocumentEvent.Update.Before. You can check the new values and the old values. With your new values, gather the info you need using the API and instead of sending the old values (say customer count, etc.) send the new values you've found before you actually perform your update.

You might check out the Recursion Control section of this article by Martin H as well.

Here is a similar question on SO with some possible solutions (although it is 2 years old).

0 votesVote for this answer Mark as a Correct answer

Jeroen Fürst answered on September 10, 2015 13:48 (last edited on September 10, 2015 13:48)

From my experience it's hard to leverage custom event handlers for this type of tasks. Especially when working with different types of workflows. I would recommend you to consider creating a custom form control (for example a button) that does exactly what you need. This gives you much more control over the outcome.

Good luck!

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on September 16, 2015 15:22

I would go with Brenden's approach, it will remain pretty easy to manage, and also you don't actually have to save anything, whatever data is changed in the before function will be carried over to the save function that Kentico does. If I hooked up a global handler before action, I could then change the value of any field, as long as the value is a valid value for the field type, and then kentico will automatically save it. This will be a lot easier to manage than a custom form control, and will also have a way better user experience, without a ton of work and customization.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.