首页 > 代码库 > Dynamics CRM 2015/2016 Web API:Unbound Action 和 Bound Action

Dynamics CRM 2015/2016 Web API:Unbound Action 和 Bound Action

上篇文章介绍了Bound/Unbound Function。今天我们来看看Action吧。像我之前说的:Function和Action之前的差别能够简单理解为。Function不改动数据,可是Action却会改动数据。

今天呢,我们也分别看看Bound Action和Unbound Action,事实上它们的调用方式和Function是一样的,仅仅是这里的请求提交方式很多其它的是採用Post的方式。我们先来看两个样例吧:

公共变量

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

它也是能够基于详细的上下文去调用的,当然在调用这个Bound Action之前,不要忘记加入前缀“Microsoft.Dynamics.CRM”,要不然。请求会失败。在以下这个样例里面,我们往队列里面加入了一个电话记录。细致的朋友肯定已经意识到了,这里我们须要构造请求体。对于请求体的数据结构,能够看博主之前的文章,在这套新API里面。数据结构都简化了,我们能够使用最基础的结构去构造请求体。

            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

顾名思义,该Action能够在不论什么实体上进行调用,能够对其单独调用,例如以下样例。

这里我们调用了WinOpportunity Action,对某个商机发起赢单操作。期间。我们是须要构造正确的请求体。

            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("opportunityid@odata.bind", 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>(await winOppResp.Content.ReadAsStringAsync());
                Console.WriteLine(result);
            }

调用这些Action是不是非常方便了,仅仅须要构造好URL和请求体就可以,调用这些请求在轻客户端是非常便捷的。至少我们不用去抓包并拼接SOAP消息体啦。


Dynamics CRM 2015/2016 Web API:Unbound Action 和 Bound Action