Dynamics CRM 2015/2016 Web API: Aggregate Query

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.