Home > MOSS 2007 > Programming under the influence

Programming under the influence

September 26th, 2007 Leave a comment Go to comments

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();
        }

Well,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();
     }

Categories: MOSS 2007 Tags:
  1. Ali B
    April 29th, 2008 at 16:12 | #1

    Neat!

  2. Roy
    April 27th, 2009 at 04:00 | #2

    Now let’s say I want to disable the eventfiring in a Windows Forms Application. How should I then approach the DisableEventFiring(). I do not want the SPListItemDeleteEvent executed. I only want to delete the item without having to go through everything that is done within the sharepoint site.

    In short: How to disableEventFiring in a Windows Forms Application

You must be logged in to post a comment.