Today, I tested whether event receiver fires or not when I use SPListItem's Update methods.
The results are here.
Methods event firing
Update() fired
SystemUpdate() fired
The results are here.
Methods event firing
Update() fired
SystemUpdate() fired
SystemUpdate(false) fired
UpdateOverwriteVersion() fired
All methods fired !!
But I think people sometimes want not to fire a event receiver when a update method is used.
So, I would like to introduce how to disable event firing.
In SharePoint 2010, SPEventReceiverBase.DisableEventFiring Method and SPEventReceiverBase.EnableEventFiring Method are already obsolete. We need to use EventFiringEnabled Property.
A sample code is here.
- private void button1_Click(object sender, EventArgs e)
- {
- const string siteUrl = "http://sp2010";
- SPSite site = new SPSite(siteUrl);
- SPWeb web = site.RootWeb;
- SPList list = web.Lists["Announcements"];
- SPListItem item = list.Items[3];
- HandleEventFiring handleEventFiring = new HandleEventFiring();
- handleEventFiring.DisableHandleEventFiring();
- try
- {
- item.Update();
- }
- finally
- {
- handleEventFiring.EnableHandleEventFiring();
- }
- }
- public class HandleEventFiring : SPItemEventReceiver
- {
- public void DisableHandleEventFiring()
- {
- //obsolete
- //this.DisableEventFiring();
- this.EventFiringEnabled = false;
- }
- public void EnableHandleEventFiring()
- {
- //obsotete
- //this.EnableEventFiring();
- this.EventFiringEnabled = true;
- }
- }
Enable to execute -ed events synchronously or asynchronously in SharePoint 2010
In SharePoint 2007, -ed events are executed only asynchronously.
This causes some problems between the main thread the and sub thread. For instance, -ed event thread sometimes cannot get its own item's "afterproperties".
Using SharePoint 2010, -ed events can be executed either synchronously or asynchronously.
The two Event Receivers are as follows:
You can write "Synchronous" in Elements.xml in SharePoint EventReciver Project in Visual Studio 2010.
Listed below are the steps for making a synchronous -ed event:
This causes some problems between the main thread the and sub thread. For instance, -ed event thread sometimes cannot get its own item's "afterproperties".
Using SharePoint 2010, -ed events can be executed either synchronously or asynchronously.
The two Event Receivers are as follows:
- -ing events (ItemAdding, ListAdding, etc)
- synchronously
- -ed events (ItemAdded, ListAdded, etc)
- synchronously or asynchronously
You can write "Synchronous" in Elements.xml in SharePoint EventReciver Project in Visual Studio 2010.
- <Synchronization>Synchronous</Synchronization>
Listed below are the steps for making a synchronous -ed event:
- Create project as Event Receiver.
- Choose 2 events (ItemAdding and ItemAdded).
- Write the code. I added the current thread name in ULS log in ItemAdding and ItemAdded method to see status of thread. It is also necessary to add Microsoft.SharePoint.Administration namespace.
- using System;
- using System.Security.Permissions;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Security;
- using Microsoft.SharePoint.Utilities;
- using Microsoft.SharePoint.Workflow;
- using Microsoft.SharePoint.Administration;
- namespace EventReceiverProject4.EventReceiver1
- {
- /// <summary>
- /// List Item Events
- /// </summary>
- public class EventReceiver1 : SPItemEventReceiver
- {
- /// <summary>
- /// An item is being added.
- /// </summary>
- public override void ItemAdding(SPItemEventProperties properties)
- {
- System.Threading.Thread.CurrentThread.Name = "Current Thread";
- string theadName = System.Threading.Thread.CurrentThread.Name;
- SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("ItemAdding", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, theadName, theadName);
- base.ItemAdding(properties);
- }
- /// <summary>
- /// An item was added.
- /// </summary>
- public override void ItemAdded(SPItemEventProperties properties)
- {
- string theadName = System.Threading.Thread.CurrentThread.Name;
- SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("ItemAdded", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, theadName, theadName);
- base.ItemAdded(properties);
- }
- }
- }
The ItemAdding and ItemAdded thread names are different because ItemAdded is run asynchronously.
The results are shown here:
- ItemAdding's thread name : Current Thread
- ItemAdded's thread name : null
Next, I added <Synchronization>Synchronous</Synchronization> in Elements.xml.
- <?xml version="1.0" encoding="utf-8"?>
- <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
- <Receivers ListTemplateId="104">
- <Receiver>
- <Name>EventReceiver1ItemAdding</Name>
- <Type>ItemAdding</Type>
- <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
- <Class>EventReceiverProject4.EventReceiver1.EventReceiver1</Class>
- <SequenceNumber>10000</SequenceNumber>
- <Synchronization>Synchronous</Synchronization>
- </Receiver>
- <Receiver>
- <Name>EventReceiver1ItemAdded</Name>
- <Type>ItemAdded</Type>
- <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
- <Class>EventReceiverProject4.EventReceiver1.EventReceiver1</Class>
- <SequenceNumber>10000</SequenceNumber>
- </Receiver>
- </Receivers>
- </Elements>
The Results are shown here:
- ItemAdding's thread name : Current Thread
- ItemAdded's thread name : Current Thread
No comments:
Post a Comment