Differentiating between content creation and update

Topics: Customizing Orchard
Sep 10, 2013 at 4:39 AM
I've configured a content part handler with calls to OnCreated and OnUpdated to perform some application specific logic when an item is created and updated.

I've noticed that while OnCreated/ing are only called when an item is created, OnUpdated/ing are called both when an item is created and when an item is updated.

I have logic that needs to specifically run only when the content item is updated. Is there a recommended approach to detecting this? At present, I'm thinking that the best way is to check the ContentItem.Version and if this is 1, then assume that the record is being created, otherwise it's an update?
Coordinator
Sep 10, 2013 at 7:47 PM
I don't follow. What's wrong with doing it on update then? Do you mean only on creation?
Coordinator
Sep 10, 2013 at 7:51 PM
in your handler, you define a local boolean flag (Created) which is set on the Created event. In the updated event, check for this flag.
Sep 11, 2013 at 12:35 AM
@bleroy The logic that I want to be performed only for updates is in the OnUpdated hander, but this is also call when a content item is created. So I need to be able to check here to eliminate new content items.

@sebastienros I had considered using a local flag, but I was a little unsure of the scope of this local variable within my handler. I expect the handler is instantiated once per http request, so is it safe to assume that only one content item would ever be created per request? What if a call to the content manager is used to create multiple content items in a single request? I guess I could create a dictionary of bool flags key by content item id?

In the end I decided to refer to the content item's Version property and if this is 1 assume it's a creation and ignore it.