Programming under the influence
Let’s say you have a list (i.e.task list ) for which you need to capture ItemAdded event using an event handler and You have to do something unusual like the following:
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public override void ItemAdded(SPItemEventProperties properties)
{
SPListItem testtask = properties.ListItem.ParentList.Items.Add();
testtask["Title"] = "You are fired!::" + DateTime.Now.ToString();
testtask["Due Date"] = DateTime.Now;
testtask["Priority"] = "(1) High";
testtask.Update();
}
Congradulations! You are trapped in a loop. How? Somebody adds a task and your event handler fires and adds another task and so on and so forths. I know this may sound stupid to do, by my point is something else here. Event handler OM is now smart enough not to let you get trapped in an infinite loop ,so recursion depth in such case is set to 10 (for itemAdded) and looping only occurs 10 times. It is really cool! Look at the picture below:
If you really have to do such a thing then at least use DisableEventFiring() and EnableEventFiring(); to stop the event handler from firing 10 times.
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public override void ItemAdded(SPItemEventProperties properties)
{
DisableEventFiring();
SPListItem testtask = properties.ListItem.ParentList.Items.Add();
testtask["Title"] = "You are fired!::" + + DateTime.Now.ToString();
testtask["Due Date"] = DateTime.Now;
testtask["Priority"] = "(1) High";
testtask.Update();
EnableEventFiring();
}