Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

share|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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.