今天我们来看看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";
Unbound Function(无參数)
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"]);
}
Unbound Function(带參数)
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"]); }
Bound Function
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改动数据