{"id":10,"date":"2007-09-26T15:34:00","date_gmt":"2007-09-26T20:34:00","guid":{"rendered":"http:\/\/blogs.devhorizon.com\/reza\/?p=10"},"modified":"2008-01-10T10:25:59","modified_gmt":"2008-01-10T15:25:59","slug":"programming-under-the-influence","status":"publish","type":"post","link":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/","title":{"rendered":"Programming under the influence"},"content":{"rendered":"<p>Let\u2019s 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:<\/p>\n<blockquote><p>[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]<br \/>\npublic override void ItemAdded(SPItemEventProperties properties)<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SPListItem testtask = properties.ListItem.ParentList.Items.Add();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 testtask[&#8220;Title&#8221;] = &#8220;You are fired!::&#8221; + DateTime.Now.ToString();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 testtask[&#8220;Due Date&#8221;] = DateTime.Now;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 testtask[&#8220;Priority&#8221;] = &#8220;(1) High&#8221;;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 testtask.Update();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/p><\/blockquote>\n<p>Well,You are trapped in a loop. How?\u00a0Somebody adds\u00a0 a task and your event handler fires and adds another task and\u00a0 so on and so forths. I know this may\u00a0sound 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:<\/p>\n<div align=\"center\"><a target=\"_blank\" href=\"http:\/\/farm2.static.flickr.com\/1330\/1443674611_1674e72345_o.jpg\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" width=\"500\" src=\"http:\/\/farm2.static.flickr.com\/1330\/1443674611_8a64ebe44f.jpg\" height=\"180\" \/><\/a><\/div>\n<p><o:p>\u00a0<\/o:p>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.<o:p>\u00a0<\/o:p><\/p>\n<blockquote><p><o:p><\/o:p>[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]<br \/>\npublic override void ItemAdded(SPItemEventProperties properties)<br \/>\n\u00a0\u00a0\u00a0\u00a0{<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0DisableEventFiring();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0SPListItem testtask = properties.ListItem.ParentList.Items.Add();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0testtask[&#8220;Title&#8221;] = &#8220;You are fired!::&#8221; + + DateTime.Now.ToString();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0testtask[&#8220;Due Date&#8221;] = DateTime.Now;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0testtask[&#8220;Priority&#8221;] = &#8220;(1) High&#8221;;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0testtask.Update();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0EnableEventFiring();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0}<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s 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) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 { \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SPListItem testtask = properties.ListItem.ParentList.Items.Add(); \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 testtask[&#8220;Title&#8221;] = &#8220;You are fired!::&#8221; + DateTime.Now.ToString(); [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[3],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-moss-2007","tag-moss-2007"],"_links":{"self":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":0,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}