Dear friends, today is the first blog post issued 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--run FetchXML query using the Web API!
That's right, guys. You have heard the error, run the FetchXML query using the Web API. In the past we do this kind of thing but it takes a Dickens, splicing a lot of SOAP message body and easy error.
Now, we can add the fetchxml we want to run in the URL, and the system will get back the data we want. Isn't it very nice!
The Web API's data query method is still relatively lightweight. Can not do complex queries, such as: Aggregate query. Group queries. But we can use FetchXML to easily implement these functions, then, the blogger will show you a simple sample: Use the Web API to query the total number of records for the current customer entity.
First of all. We want to construct a fetchxml, such as the following:
<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, such as the following:
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 with 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
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>
Dynamics CRM 2015/2016 Web API: Aggregate Query