Reza on blogging [MVP]

THIS BLOG HAS MOVED TO: http://blogs.devhorizon.com/reza

Subscriptions

<November 2008>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

News



toronto.sharepoint.camp


Navigation

Post Categories

Other Bloggers

Personal Links

Wednesday, September 26, 2007 - Posts

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


posted Wednesday, September 26, 2007 4:34 PM by rezaa with 1 Comments

Powered by Community Server, by Telligent Systems