I have a class:

public class MyEvents : SPItemEventReceiver

And a static member in it: public static int i; I need to transfer data from ItemDeliting event handler to ItemDeleted. I'm going to change the variable i in ItemDeliting and then read it in ItemDeleted. Will it all work properly?


NO, that'll not work.

A static member is shared between all instances of MyEvents in the same AppDomain, so if two Items are deleted at the same time they may override it.

If your ItemDeleted is Synchronous then it'll run in the same task as ItemDeleting and you can use a ThreadLocal variable. But if it's Asynchronous then it'll run in a separate thread so you need somewhere else to store the data.

| improve this answer | |
  • Thank you! That is, I have to write <Synchronization> Synchronous</ Synchronization> in the Elements.xml? – SDG Mar 15 '12 at 6:25
  • And add the attribute [ThreadStatic] to public class MyEvents : SPItemEventReceiver – SDG Mar 15 '12 at 6:26
  • Yes, but remember that making the ItemDeleting synchronous means that the user/program deleting the item will wait for your code to finish – Per Jakobsen Mar 15 '12 at 7:36

Tricky! Normally you would store it in the item's properties: add a hidden field to your item, and set its value in the synchronous event.

But in your case the item will be gone as you enter ItemDeleted. Is this variable required to be per item, or it can be global? You could use a property bag value on the list.

ItemDeleting and ItemDeleted run in separate threads, at separate times.

| improve this answer | |

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.