Today we take a look at the Web API data query functionality, although the previous introduction of crud article mentioned how to read the data, but did not go into the details of the details, today we will come to concrete look at it. In fact, the WEB API's data query interface is also based on the OData protocol, so the previous OData URL query construction rules have not changed very much, for example: $top, $select, $filter, $expand, $order function is still in, But something new has been added, such as
$count--Returns the total number of records
Paging mechanism (paging mechanism)--come on, now, the implementation mechanism is different, set the page size based on the HTTP header
Formatted Value (new thing, no latest document found)--visual and record formatting, but no specific mapping document found
$count
$count well understood, returns the total number of records in the system (related to the filter condition), and returns the total number of all records if the filter condition is not established.
Paging mechanism need to set HTTP header: Odata.maxpagesize=? , not the same as the previous API.
Httprequestmessage retrieveaccreq = new Httprequestmessage (httpmethod.get, Webapiurl + "/accounts? $select =name&$ Count=true "); RetrieveAccReq.Headers.Authorization = new Authenticationheadervalue ("Bearer", Accesscode); RETRIEVEACCREQ.HEADERS.ADD ("Prefer", "odata.maxpagesize=2"); String Nextpagelink = String. Empty; Jobject result = null; int pageIndex = 1; Httpresponsemessage Retrieveaccresp; do {if (!string). Isnullorwhitespace (Nextpagelink)) {retrieveaccreq = new Httprequestmessage (HTTPMETHOD.G ET, nextpagelink); RetrieveAccReq.Headers.Authorization = new Authenticationheadervalue ("Bearer", Accesscode); RETRIEVEACCREQ.HEADERS.ADD ("Prefer", "odata.maxpagesize=2"); } RETRIEVEACCRESP = await client. SendAsync (Retrieveaccreq); result = Jsonconvert.deserializeobject<joBject> (await RetrieveAccResp.Content.ReadAsStringAsync ()); Console.WriteLine ("page-" + pageindex++); Console.WriteLine (Result. ToString ()); if (Retrieveaccresp.issuccessstatuscode) {if (result["@odata. NextLink"]! = NULL &&A MP;!string. Isnullorwhitespace (result["@odata. NextLink"]. Value<string> ())) {Nextpagelink = result["@odata. NextLink"]. Value<string> (); } else {nextpagelink = ""; }} else {break; }} while (!string. IsNullOrEmpty (Nextpagelink));
formatted Value
Visual and return records of the formatting related to the returned records better understand, but did not find the specific formatted Mapping, recommended not easy to use.
Httprequestmessage retrieveaccwithformattedvaluereq = new Httprequestmessage (httpmethod.get, WebApiUrl + "/accounts?$ select=name,donotpostalmail,accountratingcode,numberofemployees,revenue& $top = 1 "); RetrieveAccWithFormattedValueReq.Headers.Authorization = new Authenticationheadervalue ("Bearer", Accesscode); RETRIEVEACCWITHFORMATTEDVALUEREQ.HEADERS.ADD ("Prefer", "odata.include-annotations=\" Odata.community.display.v1.formattedvalue\ ""); Httpresponsemessage Retrieveaccwithformatedvalueresp = await client. SendAsync (Retrieveaccwithformattedvaluereq); if (retrieveaccwithformatedvalueresp.issuccessstatuscode) {jobject result = Jsonconvert.deseria Lizeobject<jobject> (await RetrieveAccWithFormatedValueResp.Content.ReadAsStringAsync ()); Console.WriteLine (Result. ToString ()); }
In the Web API data query inside, there is a more important thing is to use the Web API query Function, bo in the following blog post for you to introduce its use.
Dynamics CRM 2015/2016 Web API: New ways to query data