今天我们来看看Dynamics CRM Web API Function 吧, 这是一个新概念,刚接触的时候我也是比较的迷糊,这样的命名确实是和之前的那套基于SOAP协议的API完全联系不上。好了,不说闲话了。这里的Function呢,就我来看,更像是一些被封装好的原生函数和老API中的Request差不多的意思,只是API的架构方式变了,所以名称也就跟着变了。
我们之前要查看当前登录用户的信息,需要调用WhoAmIRequest,那现在呢?我们需要调用WhoAmI Function。 这里的Function又被分为 Unbound和Bound,什么意思呢?Unbound Function可以理解为不专门服务某个实体,我们可以在任何时候去调用它。Bound Function 则是专门为某个实体服务,它对其他实体是不可见的,并且提供调用的方式也很便捷。
我们来看两个例子吧,一个是调用Unbound Function,另一个是调用Bound Function:
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";
HttpRequestMessage whoaiReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/WhoAmI()"); whoaiReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", auth.AcquireToken().AccessToken); HttpResponseMessage whoaiResp = await client.SendAsync(whoaiReq); if (whoaiResp.IsSuccessStatusCode) { JObject whoaiInfo = JsonConvert.DeserializeObject<JObject>(await whoaiResp.Content.ReadAsStringAsync()); Console.WriteLine(whoaiInfo["UserId"]); }
string userId = "05cc9481-7d37-4e20-8de0-dd892c47939b"; string retrievePrvQuery = string.Format(webApiUrl + "/RetrieveUserPrivileges(UserId=@p1)?@p1={0}", userId); HttpRequestMessage retrievePrvReq = new HttpRequestMessage(HttpMethod.Get, retrievePrvQuery); retrievePrvReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", auth.AcquireToken().AccessToken); HttpResponseMessage retrievePrvResp = await client.SendAsync(retrievePrvReq); if (retrievePrvResp.IsSuccessStatusCode) { JObject priveleges = JsonConvert.DeserializeObject<JObject>(await retrievePrvResp.Content.ReadAsStringAsync()); Console.WriteLine(priveleges["value"]); }
HttpRequestMessage calculateTotalTimeReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/incidents(963EF58A-75BB-E511-80D9-C4346BC43F3C)/Microsoft.Dynamics.CRM.CalculateTotalTimeIncident()"); calculateTotalTimeReq.Headers.Authorization = new AuthenticationHeaderValue(auth.AcquireToken().AccessToken); HttpResponseMessage calculateTotalTimeResp = await client.SendAsync(calculateTotalTimeReq); Console.WriteLine(calculateTotalTimeResp.IsSuccessStatusCode); Console.WriteLine(await calculateTotalTimeResp.Content.ReadAsStringAsync());
最后呢,必须唠叨的提一下Action,毕竟它们是一起的兄弟嘛。 用一句最简单的话去区分它们:Function不修改数据,Action修改数据