Synchronous Event Receivers
One of these is the ability to run after events synchronously (ItemAdded, ListAdded and WebProvisioned). Previously all after events were executed asynchronously and on a background thread in a different process. What we can do now is run these after events on demand, they are executed according to the sequence number.
I used this feature the other day when creating an Event Receiver for a picture library and it’s pretty darn useful. Here’s the scenario I had -
When a picture is added to the library a field needs to automatically update with some EXIF metadata which is extracted from the picture.
The metadata must be visible to the user in the edit form for them to see before clicking OK.
In the above scenario I couldn’t use an asynchronous event receiver because the updated value in the field would not be visible to the user in the edit form. I needed to make sure that the event receiver would run as soon as the item was added and not in a background thread, this way the new value will be visible to the user in the edit form.
In order to make your event receiver Synchronous you can set the Synchronous property of the SPEventReceiverDefinition class (if you are binding programmatically). Or by creating a node in yourXML, see below for examples of each -
Programmatically
1
2
3
4
5
6
7
8
9
10
| MyEventReceiver eventReceiver = new MyEventReceiver(); SPEventReceiverDefinitionCollection eventReceiverCol = myList.EventReceivers; SPEventReceiverDefinition eventReceiverDef = eventReceiverCol.Add(); eventReceiver.Name = “My Event Receiver”; eventReceiver.Synchronization = SPEventReceiverSyncronization.Synchronous; eventReceiver.Type = SPEventReceiverType.ItemAdded; eventReceiver.SequenceNumber = 100; eventReceiver.Assembly = Assembly.GetExecutingAssembly().FullName; eventReceiver.Class = eventReceiver.GetType().ToString(); eventReceiver.Update(); |
1
2
3
4
5
6
7
8
9
10
| < Receivers > < Receiver > < Name >MyEventReceiver</ Name > < Type >ItemAdded</ Type > < Assembly >$SharePoint.Project.AssemblyFullName$</ Assembly > < Class >MySolution.EventReceivers.MyEventReceiver</ Class > < SequenceNumber >100</ SequenceNumber > < Synchronization >Synchronous</ Synchronization > </ Receiver > </ Receivers > |