I had an event receiver applied on documents wich can start, programatically of course, a workflow. In this workflow, i used to update some metadatas in my document item. However that triggers the event receiver a new time and you can imagine the infinate loop. Well! afeter googling i found that it is possible to diable the event firing just before the item.Update or the item.SystemUpdate.
In first time i used this method but i didn't made my code in a try catch bloc. After doing that my workflow runs normally and it didn't fires the events in the event receiver. Here is the code :
In first time i used this method but i didn't made my code in a try catch bloc. After doing that my workflow runs normally and it didn't fires the events in the event receiver. Here is the code :
public static class SPListItemExtensions
{
///
/// Provides ability to update list item without firing event receiver.
///
///
/// Disables firing /// event receiver while updating item.
public static void Update(this SPListItem item, bool doNotFireEvents)
{
SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
if (doNotFireEvents)
{
try
{
rh.DisableEventFiring();
item.Update();
}
finally
{
rh.EnableEventFiring();
}
}
else
{
item.Update();
}
}
///
/// Provides ability to update list item without firing event receiver.
///
///
///
/// Disables firing event receiver while updating ///item.
public static void SystemUpdate(this SPListItem item, bool incrementListItemVersion, bool doNotFireEvents)
{
SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
if (doNotFireEvents)
{
try
{
rh.DisableEventFiring();
item.SystemUpdate(incrementListItemVersion);
}
finally
{
rh.EnableEventFiring();
}
}
else
{
item.SystemUpdate(incrementListItemVersion);
}
}
///
/// Provides ability to update list item without firing event receiver.
///
///
/// Disables firing event receiver while updating item.
public static void SystemUpdate(this SPListItem item, bool doNotFireEvents)
{
SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
if (doNotFireEvents)
{
try
{
rh.DisableEventFiring();
item.SystemUpdate();
}
finally
{
rh.EnableEventFiring();
}
}
else
{
item.SystemUpdate();
}
}
private class SPItemEventReceiverHandling : SPItemEventReceiver
{
public SPItemEventReceiverHandling() { }
new public void DisableEventFiring()
{
base.DisableEventFiring();
}
new public void EnableEventFiring()
{
base.EnableEventFiring();
}
}
}
I had a headache when I was looking for a way to update my workflow task without firing the OnTaskChanged event. I found a solution but this one, let's say it, is more beautiful :).
ReplyDeleteThanks