{"id":1216,"date":"2011-07-24T13:01:41","date_gmt":"2011-07-24T11:01:41","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=1216"},"modified":"2015-09-08T20:48:37","modified_gmt":"2015-09-08T18:48:37","slug":"to-this-or-not-to-this-with-the-client-object-model","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/to-this-or-not-to-this-with-the-client-object-model\/","title":{"rendered":"To &#8216;this&#8217; or not to &#8216;this&#8217; with the Client Object model"},"content":{"rendered":"<p>Recently I developed a CustomAction on a list with some client script in the CommandAction. Nothing fancy or new.<br \/>\nI put a .js file in the _layouts folder and referenced the file by a CustomAction with Location &#8216;ScriptLink&#8217; and ScriptSrc. Put some code in the file to get some data I needed and called executeQueryAsync with two delegates like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclientContext.executeQueryAsync(Function.createDelegate(this, this.OnSucceeded), Function.createDelegate(this, this.OnFailed));\r\n<\/pre>\n<p>Deployed as a Farm solution on the environment and everything went well so far.<\/p>\n<p>After this, minds changed and it had to become a sandboxed solution. Ok, let&#8217;s do it.<br \/>\n1. Set the Sandboxed Solution property to True on the project<br \/>\n2. Removed the CustomAction with Location=&#8217;ScriptLink&#8217;<br \/>\n3. Put the code from the js file from the _layouts folder in the CommandUIHandler&#8217;s attribute CommandAction<br \/>\n4. Deployed the sandboxed solution<\/p>\n<p>I always use FireFox when programming and all of a sudden I received the following error:<br \/>\n<strong>b is undefined<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1222\" title=\"Client object model this FireFox\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/07\/Blog-item-client-object-model-this-FireFox-msg-small.png\" alt=\"Client object model this FireFox\" width=\"599\" height=\"46\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/07\/Blog-item-client-object-model-this-FireFox-msg-small.png 599w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/07\/Blog-item-client-object-model-this-FireFox-msg-small-300x23.png 300w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/p>\n<p>and things stopped working. While in the Farm solution the CustomAction worked as expected and the OnSucceeded and OnFailed were executed.<\/p>\n<p>I put in some alert statements to see where the code broke and it seemed the OnSucceeded and the OnFailed methods weren&#8217;t executed at all.<br \/>\nBy removing the &#8216;this&#8217; keyword in the createDelegate everything went well again:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclientContext.executeQueryAsync(Function.createDelegate(this, OnSucceeded), Function.createDelegate(this, OnFailed));\r\n<\/pre>\n<p>This behaviour seems a bit strange to me. When putting the code in a separate js file and deploying it to the _layouts folder the &#8216;this&#8217; keyword can be used, but putting the exact same code in the CommandAction of CommandUIHandler the OnSucceeded and OnFailed methods can&#8217;t be found anymore using the &#8216;this&#8217; keyword.<\/p>\n<p>By the way, in IE you will receive the following error:<br \/>\n<strong>&#8216;b&#8217; is null or not an object<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1224\" title=\"Client object model this IE\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/07\/Blog-item-client-object-model-this-IE-msg1.png\" alt=\"Client object model this IE\" width=\"553\" height=\"200\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/07\/Blog-item-client-object-model-this-IE-msg1.png 553w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/07\/Blog-item-client-object-model-this-IE-msg1-300x108.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I developed a CustomAction on a list with some client script in the CommandAction. Nothing fancy or new. I put a .js file in the _layouts folder and referenced the file by a CustomAction with Location &#8216;ScriptLink&#8217; and ScriptSrc. &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/to-this-or-not-to-this-with-the-client-object-model\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[25,42,6],"class_list":["post-1216","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-sandboxed-solutions","tag-sharepoint-2010","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=1216"}],"version-history":[{"count":7,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1216\/revisions"}],"predecessor-version":[{"id":1227,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1216\/revisions\/1227"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=1216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=1216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=1216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}