Dear friends, today is the first article published by bloggers in 2016, first of all, I wish you a happy New year, pay double, haha. Today we look at a more important feature--use the Web API to execute FetchXML queries! Yes, guys, you heard it wrong. Use the Web API to execute FetchXML queries. In the past we have done this kind of thing to spend Dickens yo, splicing a lot of SOAP message body and also error-prone. Now, we can add in the URL we want to execute the FetchXML can, the system will be obediently to the data we want to return back, is not very good!
At the same, the Web API data query method is relatively lightweight, or can not do complex queries, such as: aggregate queries, group queries. But we can use FetchXML to easily implement these functions, and then, the blogger will show you a simple small example: Use the Web API to query the total number of records for the current customer entity.
First of all, we are going to construct a fetchxml, as follows:
<fetch aggregate= "true" > <entity name= "Account" > <attribute name= "AccountId"/> < Attribute name= "Name" aggregate= "Count" alias= "ct"/> </entity></fetch>
then we'll stitch this fetchxml into the Web API's request URL, as follows:
https://ghostbear.api.crm6.dynamics.com/api/data/v8.0/accounts?fetchXml=%3Cfetch%20aggregate=%22true%22%3E% 3centity%20name=%22account%22%3e%3cattribute%20name=%22name%22%20aggregate=%22count%22%20alias=%22ct%22/%3e%3c /entity%3e%3c/fetch%3e
Finally, we call it through the following code and return the result:
Httprequestmessage acccountreq = new Httprequestmessage (httpmethod.get, Webapiurl + "/accounts?fetchxml=%3cfetch% 20aggregate=%22true%22%3e%3centity%20name=%22account%22%3e%3cattribute%20name=%22name%22%20aggregate=%22count% 22%20alias=%22ct%22/%3e%3c/entity%3e%3c/fetch%3e "); AccCountReq.Headers.Authorization = new Authenticationheadervalue ("Bearer", Accesscode); Httpresponsemessage Acccountresp = await client. SendAsync (acccountreq); if (Acccountresp.issuccessstatuscode) { Jobject result = jsonconvert.deserializeobject<jobject> ( Await AccCountResp.Content.ReadAsStringAsync ()); Console.WriteLine (Result. ToString ()); }
return Results
Dynamics CRM 2015/2016 Web API: Aggregate query