Reza on blogging [MVP]

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

Subscriptions

<October 2008>
SuMoTuWeThFrSa
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

News



toronto.sharepoint.camp


Navigation

Post Categories

Other Bloggers

Personal Links

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 on Wednesday, September 26, 2007 4:34 PM by rezaa

# SharePoint Links 09.27.07 @ Thursday, October 11, 2007 8:46 PM

Some important SharePoint posts making the rounds in the last few days:

Joel shares his thoughts on the 2 Stage Recycle Bin and recovery and some common misconceptions / mistakes

Shane Young asks if Kerberos is case sensitive and the steps in setting...

Anonymous

Powered by Community Server, by Telligent Systems