博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dynamics CRM 2015/2016 Web API:新的数据查询方式
阅读量:7029 次
发布时间:2019-06-28

本文共 3296 字,大约阅读时间需要 10 分钟。

今天我们来看看Web API的数据查询功能,尽管之前介绍CRUD的文章里面提到过怎么去Read数据,可是并没有详细的去深究那些细节,今天我们就来详细看看吧。事实上呢,Web API的数据查询接口也是基于OData协议的,所以之前的OData Url Query的构造规则没有非常大的变化。比如:$top, $select, $filter, $expand, $order的功能还是在的,只是也加入了一些新东西,比如

$count  -- 返回记录的总数

Paging Mechanism(分页机制)-- 来东西,如今,实现机制不一样了,基于HTTP报头设置页大小

Formatted Value(新东西。没有找到最新的文档) -- 目測和记录的格式化有关,只是没找到详细的Mapping文档

$count

$count非常好理解,返回记录在系统中的总数(和filter条件相关),假设不制定filter条件。则返回全部记录的总数。

Paging Mechanism

须要设置HTTP 报头:odata.maxpagesize=?,和之前的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.Get, nextPageLink);                    retrieveAccReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode);                    retrieveAccReq.Headers.Add("Prefer", "odata.maxpagesize=2");                }                retrieveAccResp = await client.SendAsync(retrieveAccReq);                result = JsonConvert.DeserializeObject
(await retrieveAccResp.Content.ReadAsStringAsync()); Console.WriteLine("Page-" + pageIndex++); Console.WriteLine(result.ToString()); if (retrieveAccResp.IsSuccessStatusCode) { if (result["@odata.nextLink"] != null && !string.IsNullOrWhiteSpace(result["@odata.nextLink"].Value
())) { nextPageLink = result["@odata.nextLink"].Value
(); } else { nextPageLink = ""; } } else { break; } } while (!string.IsNullOrEmpty(nextPageLink));

Formatted Value

目測和返回记录的格式化有关,返回的记录更好理解,只是没找到详细的Formatted Mapping。建议不轻易使用。

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.DeserializeObject
(await retrieveAccWithFormatedValueResp.Content.ReadAsStringAsync()); Console.WriteLine(result.ToString()); }
在Web API的数据查询里面呢,另一个比較重要的仅仅是就是使用Web API Query Function,博主将在兴许的博文中为大家介绍它的用法。

你可能感兴趣的文章
雷军定AI+IoT为小米核心战略,牵手宜家推进生态布局
查看>>
书评:《All About Java 8 Lambdas》
查看>>
搜狗信息流推荐算法实践
查看>>
Visual Studio 2017 15.6发布
查看>>
2019年Java和JVM生态系统预测:OpenJDK将成为Java运行时市场领导者
查看>>
拥抱PostgreSQL,红帽再表态:SSPL的MongoDB坚决不用
查看>>
架构设计复杂度的6个来源
查看>>
360首席安全官谭晓生宣布离职
查看>>
在敏捷中应用测试驱动开发
查看>>
到底谁应该对软件开发的质量负责?
查看>>
微软Windows Core OS被曝应用了开源组件
查看>>
用Elm语言降低失败的风险
查看>>
资深专家都知道的Docker常用命令
查看>>
谈谈UCloud的秒级在线快照服务
查看>>
Spring Web Services 3.0.4.RELEASE和2.4.3.RELEASE发布
查看>>
EGO走进美团——追寻千亿市场背后的技术力量
查看>>
腾讯正式宣布成立技术委员会,要对组织架构下狠手
查看>>
3·15曝光丨智能机器人一年拨打40亿个骚扰电话,6亿人信息已遭泄露!
查看>>
腾讯携手中科院国家天文台落地FAST 用云计算探索星辰大海
查看>>
随机森林算法4种实现方法对比测试:DolphinDB速度最快,XGBoost表现最差
查看>>