Dynamics CRM 2015/2016 Web API: View-based Data Query
Dynamics CRM 2016 Web API supports view-based data query. The Feature that the blogger saw at the beginning has really highlighted me. This Feature is so powerful, this completely subverts the complicated and tedious method of getting data. The current view is actually defined once and reused everywhere! It is easy to use. You only need to add a parameter and specify the View ID to be called. Next, I will show you three common scenarios: calling public views, calling private views, and calling SubGrid views.
Public View
What is a public view? This is the view that everyone has access permissions. For example, the view of my available customers on the customer entity is called on the UI.
Call Code
HttpRequestMessage getPredefinedQueryIdReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/savedqueries?$filter=name eq 'My Active Accounts'"); getPredefinedQueryIdReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode); HttpResponseMessage getPredefinedQueryIdResp = await client.SendAsync(getPredefinedQueryIdReq); if (getPredefinedQueryIdResp.IsSuccessStatusCode) { JObject result = JsonConvert.DeserializeObject
(await getPredefinedQueryIdResp.Content.ReadAsStringAsync()); string queryId = result["value"][0]["savedqueryid"].Value
(); HttpRequestMessage getAccByQueryViewReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/accounts?savedQuery=" + queryId); getAccByQueryViewReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode); HttpResponseMessage getAccByQueryViewResp = await client.SendAsync(getAccByQueryViewReq); if (getAccByQueryViewResp.IsSuccessStatusCode) { JObject result2 = JsonConvert.DeserializeObject
(await getAccByQueryViewResp.Content.ReadAsStringAsync()); Console.WriteLine(result2.ToString()); } }
Private view
Private views are self-defined views for our own use. We generally call them on the UI.
Call Code
HttpRequestMessage getUserQueryIdReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/userqueries?$filter=name eq 'Jeffs View'"); getUserQueryIdReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode); HttpResponseMessage getUserQueryIdResp = await client.SendAsync(getUserQueryIdReq); if (getUserQueryIdResp.IsSuccessStatusCode) { JObject result = JsonConvert.DeserializeObject
(await getUserQueryIdResp.Content.ReadAsStringAsync()); string queryId = result["value"][0]["userqueryid"].Value
(); HttpRequestMessage getAccByQueryViewReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/accounts?userQuery=" + queryId); getAccByQueryViewReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode); HttpResponseMessage getAccByQueryViewResp = await client.SendAsync(getAccByQueryViewReq); if (getAccByQueryViewResp.IsSuccessStatusCode) { JObject result2 = JsonConvert.DeserializeObject
(await getAccByQueryViewResp.Content.ReadAsStringAsync()); Console.WriteLine(result2.ToString()); } }
SubGrid View
Cascading view, which is a view that filters data based on the context of the data. We usually call them on the UI.
Call Code
HttpRequestMessage getDataBySubGridViewReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/accounts(823ef58a-75bb-e511-80d9-c4346bc43f3c)/contact_customer_accounts/?savedQuery=00000000-0000-0000-00aa-000010001033"); getDataBySubGridViewReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode); HttpResponseMessage getDataBySubGridViewResp = await client.SendAsync(getDataBySubGridViewReq); if (getDataBySubGridViewResp.IsSuccessStatusCode) { JObject result = JsonConvert.DeserializeObject
(await getDataBySubGridViewResp.Content.ReadAsStringAsync()); Console.WriteLine(result.ToString()); }