1. 程式人生 > >Dynamics CRM 2015/2016 Web API:Unbound Action 和 Bound Action

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

account token com red wait font content soap消息 clas

上篇文章介紹了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("[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>(await winOppResp.Content.ReadAsStringAsync()); Console.WriteLine(result); }
調用這些Action是不是非常方便了,僅僅須要構造好URL和請求體就可以,調用這些請求在輕客戶端是非常便捷的。至少我們不用去抓包並拼接SOAP消息體啦。


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