The previous article introduced Bound/unbound Function, today we look at action, as I said before: function and action before the difference can be understood as simple, function does not modify the data, but the action will modify the data. Today, we also look at bound action and unbound action, in fact, they call the same way and function is the same, but here is the request to submit the way more is the use of post, we first look at two examples:
Public variables
static string clientId = "580C20BE-5960-42A0-837F-9B554B88B2D5";//"025220cd-a8c9-414f-aad7-a9288404262b"; static string service = "https://ghostbear.crm6.dynamics.com"; static string RedirectURL = "Http://localhost/weapidemo"; static string username = "Account"; static string password = "pwd"; static string Webapiurl = "https://ghostbear.api.crm6.dynamics.com/api/data/v8.0";
Bound Action
It can also be called based on a specific context, but before calling this bound action, do not forget to add the prefix "Microsoft.Dynamics.CRM", otherwise the request will fail. In the following example, we add a phone record to the queue. Careful friends must have realized, here we need to construct the request body, for the data structure of the request body, you can see the blog before the article, in this new API, the data structure is simplified, we can use the most basic structure to construct the request body.
Httprequestmessage addtoqueuereq = new Httprequestmessage (httpmethod.post, Webapiurl + "/queues (70f6a997-71bb- E511-80D8-C4346BC5F7C8)/microsoft.dynamics.crm.addtoqueue "); AddToQueueReq.Headers.Authorization = new Authenticationheadervalue ("Bearer", auth. Acquiretoken (). Accesstoken); Jobject addtoqueueactivity = new Jobject (); Addtoqueueactivity.add ("ActivityID", "3040f58a-75bb-e511-80d9-c4346bc43f3c"); Addtoqueueactivity.add ("@odata. Type", "Microsoft.Dynamics.CRM.phonecall"); Jobject addtoqueuecontent = new Jobject (); Addtoqueuecontent.add ("Target", addtoqueueactivity); Addtoqueuereq.content = new Stringcontent (Jsonconvert.serializeobject (addtoqueuecontent), Encoding.UTF8, " Application/json "); Httpresponsemessage Addtoqueueresp = await client. SendAsync (Addtoqueuereq); if (addtoqueueresp.issuccessstatuscode) {jobject result = Jsonconvert.deserializeobjEct<jobject> (await AddToQueueResp.Content.ReadAsStringAsync ()); Console.WriteLine (result["Queueitemid"]); }
Unbound Action
As the name implies, the action can be called on any entity and can be called separately, as in the following example. Here we call the Winopportunity action to initiate a win action on an opportunity, during which we need to construct the correct request body.
Httprequestmessage winoppreq = new Httprequestmessage (httpmethod.post, Webapiurl + "/winopportunity"); WinOppReq.Headers.Authorization = new Authenticationheadervalue ("Bearer", auth. Acquiretoken (). Accesstoken); Jobject Closeoppitem = new Jobject (); Closeoppitem.add ("Subject", "Won opportunity!"); Closeoppitem.add ("[email protected]", Webapiurl + "/opportunities (1440f58a-75bb-e511-80d9-c4346bc43f3c)"); Jobject requestbody = new Jobject (); Requestbody.add ("Status", 3); Requestbody.add ("Opportunityclose", Closeoppitem); Winoppreq.content = new Stringcontent (Jsonconvert.serializeobject (requestbody), Encoding.UTF8, "Application/json"); Httpresponsemessage Winoppresp = await client. SendAsync (Winoppreq); if (winoppresp.issuccessstatuscode) {jobject result = jsonconvert.deserializeobject<jobject& gt; (await WinOppResp.Content.ReadAsStringAsyNC ()); Console.WriteLine (result); }
It is convenient to call these actions, only need to construct a good URL and the request body, call these requests in the light client is very convenient, at least we do not have to grab the packet and splicing the SOAP message body.
Dynamics CRM 2015/2016 Web api:unbound action and Bound action