Dynamics CRM 2015/2016 Web API: Aggregate Query

Source: Internet
Author: User

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

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.