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
These are interesting results !!
No comments:
Post a Comment