Sometimes you need to react on item changed event and maybe you want program some application logic then. But problem is when you want to update item inside onItemChanged activity, probably you would do something like that (or maybe just I did it at first time :) )
workflowProperties.Item["Title"] = "Title was changed";
workflowProperties.Item.Update();
This particular code would seems to work fine but new onItemChange event is fired over and over. And it really isn't good solution I think. If you don't want to save just simple string like I did above. And if you want to interact with some particular changes in item, it would probably cause some mess with your application logic because of executing onItemChanged code.
You could easily use SystemUpdate() function to prevent event firing
workflowProperties.Item.SystemUpdate();
but unlucky for me it doesn't work at all.
As always there is another solution to achieve this. You can make your own Event firing class to disable event firing.
namespace CustomNamespace
{
public class EventFiringOperations : SPItemEventReceiver
{
public EventFiringOperations()
{
}
public void disableEventFiring()
{
//Obsolete way
//this.DisableEventFiring();
//Better (VS2010) (Visual studio would tell you for sure ;) )
this.EventFiringEnabled = false;
}
public void enableEventFiring()
{
//Obsolete
//this.EnableEventFiring();
//Better
this.EventFiringEnabled = false;
}
}
public class CustomClass
{
//...
EventFiringOperations eventFiring = new EventFiringOperations();
eventFiring.disableEventFiring();
item.Update(); //Now you can update :)
eventFiring.enableEventFiring();
//...
}
}
This won't help you only if you will update item inside onItemChanged activity code, but whenever you don't want to fire event when you updating item.
I have posted it here more for my benefit, but if anyone finds it useful then great.
No comments:
Post a Comment