6

I have an interesting question about event receivers.

I have long running ItemAdded event. But as soon as item is saved, another or the same user can edit it, even before async event finished. The item also has attached ItemUpdated event. Does Updated event fire after Added event is finished, or can they run simultaneusly? Because it could cause conflicts, if second event runs before first finishes. In that case I have to implement extra safeguards in ItemUpdating.

I think that would be a larger problem, if asynchronous events can run simultaneously on the same item. Because even editing one item twice in short time could lead to conflicts if both events fire at the same time.

  • 3
    Yupp that is the big problem with the -ed events, they are by default run Async, and hence update conflicts can occur. You can add Synchronous to the definition of the Event Receiver though. – Robert Lindgren Mar 13 '14 at 8:33
1

Updated only gets fired after the added event on document libraries because first the file will be uploaded (Added) and then the file will be updated with the additional information such as meta data. On lists only the item added event receiver will be fired because no update of the list item happened after the list item was written to the list.

  • You misunderstood. The user first creates item. ItemAdded fires. Then edits it again and saves. In the meantime, ItemAdded is still running. My question is, what happens to ItemUpdated. – Tschareck Mar 13 '14 at 13:06
  • There must be massive code in the item added event receiver if it is still running then or am I once again wrong? – Stefan Bauer Mar 13 '14 at 13:22
  • It creates a web based on item - takes up to 1 minute... – Tschareck Mar 13 '14 at 13:25
  • Now we found your real problem ;) You should create a web in an item added event receiver. Better consider to user a item adding event receiver and lead the user in a log running operation. msdn.microsoft.com/en-us/library/ee395906(v=office.12).aspx Works in SharePoint 2010 / 2013 the same way. After this is done save the item. This avoid the user from editing the item before you finished your work. Other methods is to use a web provisioning handler msdn.microsoft.com/en-us/library/… – Stefan Bauer Mar 13 '14 at 14:01
0

If you have long running operations its also better to use the Workflow instead of event receiver. Event receiver are meant to execute simple operations like updating metadata or calling a list etc. For any long running operations you must avoid the event receivers.

Back to your question, the item added should be completed before updated is executed. Added, Updated are async operations they executed in parallel. So, you must do below: in Item Added, you update a column (may be a hidden field) in the same list before it completed (may be as a last statement). In Item Updated get the current item and read that field, if that field is updated with the column which means item added is already ran, otherwise just sleep the thread for couple of seconds and check till some certain time and then execute item updated remaining statements.

This will be not an ideal solution as you are waiting long on the event operation. So, recommended approach is to go for a workflow.

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.