{"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"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Programming under the influence - Reza Alirezaei&#039;s Blog %<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Reza Alirezaei\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/26\\\/programming-under-the-influence\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/26\\\/programming-under-the-influence\\\/\"},\"author\":{\"name\":\"Reza Alirezaei\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"headline\":\"Programming under the influence\",\"datePublished\":\"2007-09-26T20:34:00+00:00\",\"dateModified\":\"2008-01-10T15:25:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/26\\\/programming-under-the-influence\\\/\"},\"wordCount\":219,\"commentCount\":3,\"image\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/26\\\/programming-under-the-influence\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/farm2.static.flickr.com\\\/1330\\\/1443674611_8a64ebe44f.jpg\",\"keywords\":[\"MOSS 2007\"],\"articleSection\":[\"MOSS 2007\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/26\\\/programming-under-the-influence\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/26\\\/programming-under-the-influence\\\/\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/26\\\/programming-under-the-influence\\\/\",\"name\":\"Programming under the influence - Reza Alirezaei's Blog %\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/26\\\/programming-under-the-influence\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/26\\\/programming-under-the-influence\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/farm2.static.flickr.com\\\/1330\\\/1443674611_8a64ebe44f.jpg\",\"datePublished\":\"2007-09-26T20:34:00+00:00\",\"dateModified\":\"2008-01-10T15:25:59+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/26\\\/programming-under-the-influence\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/26\\\/programming-under-the-influence\\\/#primaryimage\",\"url\":\"http:\\\/\\\/farm2.static.flickr.com\\\/1330\\\/1443674611_8a64ebe44f.jpg\",\"contentUrl\":\"http:\\\/\\\/farm2.static.flickr.com\\\/1330\\\/1443674611_8a64ebe44f.jpg\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/\",\"name\":\"Reza Alirezaei's Blog\",\"description\":\"Blogging from the field!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\",\"name\":\"Reza Alirezaei\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"caption\":\"Reza Alirezaei\"},\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/author\\\/rezaa\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Programming under the influence - Reza Alirezaei's Blog %","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/","twitter_misc":{"Written by":"Reza Alirezaei","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/#article","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/"},"author":{"name":"Reza Alirezaei","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"headline":"Programming under the influence","datePublished":"2007-09-26T20:34:00+00:00","dateModified":"2008-01-10T15:25:59+00:00","mainEntityOfPage":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/"},"wordCount":219,"commentCount":3,"image":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/#primaryimage"},"thumbnailUrl":"http:\/\/farm2.static.flickr.com\/1330\/1443674611_8a64ebe44f.jpg","keywords":["MOSS 2007"],"articleSection":["MOSS 2007"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/","url":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/","name":"Programming under the influence - Reza Alirezaei's Blog %","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/#primaryimage"},"image":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/#primaryimage"},"thumbnailUrl":"http:\/\/farm2.static.flickr.com\/1330\/1443674611_8a64ebe44f.jpg","datePublished":"2007-09-26T20:34:00+00:00","dateModified":"2008-01-10T15:25:59+00:00","author":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/26\/programming-under-the-influence\/#primaryimage","url":"http:\/\/farm2.static.flickr.com\/1330\/1443674611_8a64ebe44f.jpg","contentUrl":"http:\/\/farm2.static.flickr.com\/1330\/1443674611_8a64ebe44f.jpg"},{"@type":"WebSite","@id":"https:\/\/blogs.devhorizon.com\/reza\/#website","url":"https:\/\/blogs.devhorizon.com\/reza\/","name":"Reza Alirezaei's Blog","description":"Blogging from the field!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blogs.devhorizon.com\/reza\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938","name":"Reza Alirezaei","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","caption":"Reza Alirezaei"},"url":"https:\/\/blogs.devhorizon.com\/reza\/author\/rezaa\/"}]}},"_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}]}}