Is it possible to use SPWeb.ProcessBatchData() and not trigger all the event handlers attached to a list?
I would like to process a large list in quick time, but I don't want all the event handlers attached to the list getting fired
|
Is it possible to use SPWeb.ProcessBatchData() and not trigger all the event handlers attached to a list? I would like to process a large list in quick time, but I don't want all the event handlers attached to the list getting fired |
|||
|
|
|
We faced the same problem at work recently. And there are two good options here we've found: 1. Global variable Use SPWeb.Properties or some other SharePoint property bag, and use kind of "global variable" trick: you set this variable to "ON" before the batch is executed, and set it to "OFF" later when the batch is finished. And in your event receivers, obviously, you will need to check the value of this property, and break the execution if it is set to "ON". If you have your event receivers code and the batch code in same assembly, you may consider to use some static field (for example, in the event receiver class) for this purpose, rather than persistent property storage. 2. DisabledEventFiringScope The second approach is much more tricky and looks like a hack, but it actually can save you, if you don't have access to the receivers code, or if you have your receivers classes clattered over different solutions and you don't want to manage them, etc. The point here is that when you use EventFiringEnabled in some event receiver, actually it disables execution of event receivers in the whole thread (because internally, this functionality uses ThreadStatic variable). Having this knowledge in mind, you can implement your own IDisposable class, inherit it from SPEventReceiverBase class, disable events in its constructor, and enable them back in the Dispose method. Thus, you will be able to use the DisabledEventFiringScope class following way:
And here is the code for the DisabledEventFiringScope class:
(this approach is based on the following article: http://adrianhenke.wordpress.com/2010/01/29/disable-item-events-firing-during-item-update/) |
|||||
|
|
Theoretically, it might happen situation than you prevented event firing and user added item throught web interface exactly at this time. So, user added item wouldn't be processed by the receivers, but it have to. I think, it's better to check event receiver variable at the SPListItem level. For example, you can add hidden field to list and fill it in batch adding xml. So, you can check does this item added throught batch or throught web ui. |
|||||||||
|