{"id":2195,"date":"2016-04-06T06:01:55","date_gmt":"2016-04-06T06:01:55","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2195"},"modified":"2016-04-06T06:15:00","modified_gmt":"2016-04-06T06:15:00","slug":"using-web-api-actions-in-crm-2016","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2195","title":{"rendered":"Using Web API Actions in CRM 2016"},"content":{"rendered":"<p>In our last Web API article we discussed about Web API functions, if you have not checked it yet, <a href=\"https:\/\/himbap.com\/blog\/?p=2134\">click here <\/a>to know about web api functions. Today we are going to discuss about Web API actions. Similarly to function actions are also reusable piece of code and can bound or unbound. There are list of pre define actions, which can be referred<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt607829.aspx\"> from here<\/a>, but we can also create our own custom action and call them as well. If you are new to actions, please refer our older article to learn about <a href=\"https:\/\/himbap.com\/blog\/?p=1540\">creating actions.<\/a><\/p>\n<p>We are going to discuss about <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt607495.aspx\"><strong>AddMemberList <\/strong><\/a>action which is a bound action. Bound action or functions are called by using full name of the action or function including Microsoft.Dynamics.CRM namespace, so we need to use it like following:<br \/>\n<strong>Microsoft.Dynamics.CRM.AddMemberList<br \/>\n<\/strong><br \/>\n<strong>AddMemberList <\/strong>action takes one following parameter:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/addmemberaction.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-2199\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/addmemberaction-300x22.png\" alt=\"addmemberaction\" width=\"300\" height=\"22\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/addmemberaction-300x22.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/addmemberaction.png 580w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Actually for every web api action there is a corresponding organization request, for example <strong>AddMemberList <\/strong>has corresponding <strong><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.crm.sdk.messages.addmemberlistrequest.aspx\">AddMemberListRequest<\/a><\/strong>. If you will check this request you will see it needs three following required properties:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/addmemberactionrequest.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-2200\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/addmemberactionrequest-300x40.png\" alt=\"addmemberactionrequest\" width=\"300\" height=\"40\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/addmemberactionrequest-300x40.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/addmemberactionrequest.png 579w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>And this request can be implemented using following server side code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n AddMemberListRequest request = new AddMemberListRequest()\r\n                  { \r\n                      ListId=new Guid (&quot;&lt;&lt;GUID of the target list&gt;&gt;&quot;),\r\n                      EntityId = new Guid(&quot;&lt;&lt;GUID of the member that we want to add&gt;&gt;&quot;)\r\n                   };\r\n                  organizationService.Execute(request);\r\n<\/pre>\n<p>As you see we need to set basically two properties, listid where we want to add member and the entity id, which we want to add. Ok so back to web api, to implement this action using web api, we need to pass these two properties only. We can use following POST request\u00a0to implement it:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction AddMemberToList() {\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n    var data = {\r\n        &quot;EntityId&quot;: &quot;&lt;&lt;GUID of the member that we want to add&gt;&gt;&quot;\r\n    };\r\n\r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;POST&quot;, serverURL + &quot;\/api\/data\/v8.0\/lists(&lt;&lt;GUID of the target list&gt;&gt;)\/Microsoft.Dynamics.CRM.AddMemberList&quot;, true);\r\n    req.setRequestHeader(&quot;Accept&quot;, &quot;application\/json&quot;);\r\n    req.setRequestHeader(&quot;Content-Type&quot;, &quot;application\/json; charset=utf-8&quot;);\r\n    req.setRequestHeader(&quot;OData-MaxVersion&quot;, &quot;4.0&quot;);\r\n    req.setRequestHeader(&quot;OData-Version&quot;, &quot;4.0&quot;);\r\n    req.onreadystatechange = function() {\r\n        if (this.readyState == 4 \/* complete *\/ ) {\r\n            req.onreadystatechange = null;\r\n            if (this.status == 200) {\r\n                alert(&quot;Member added to Marketing list&quot;);\r\n            } else {\r\n                var error = JSON.parse(this.response).error;\r\n                alert(error.message);\r\n            }\r\n        }\r\n    };\r\n    req.send(JSON.stringify(data));\r\n}\r\n<\/pre>\n<p>Now we can create java script web resource and use above code. We can call AddMemeberToList method on some event. Make sure you are passing member id based on the list member selected while creating marketing list, for example if you have created marketing list for lead, so you need to pass lead id:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/marketinglist.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-2198\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/marketinglist-300x245.png\" alt=\"marketinglist\" width=\"300\" height=\"245\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/marketinglist-300x245.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/marketinglist.png 399w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>In next article we will discuss how we can call our custom action using web api so stay tuned !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last Web API article we discussed about Web API functions, if you have not checked it yet, click here to know about web api functions. Today we are going to discuss about Web API actions. Similarly to function actions are also reusable piece of code and can bound or unbound. There are list of pre define actions, which&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2195\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,275,296],"tags":[346,349,347,348],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2195"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2195"}],"version-history":[{"count":8,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2195\/revisions"}],"predecessor-version":[{"id":2206,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2195\/revisions\/2206"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}