Dynamics CRM 2015/2016新特性之二十一:使用Web API查询记录
作者:互联网
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复204或者20160310可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!
从Dynamics CRM 2016开始一次查询返回最多记录从 50 条增加到 5000条,变成以前的100倍了!而且还增加了count来统计符合条件的记录数量这个查询操作符,还是上例子比较好:
http://lycrmvm.cloudapp.net:5555/Demo/api/data/v8.0/accounts(CE23165A-3AA3-E511-80C7-000D3A807EC7)/contact_customer_accounts?$select=fullname,telephone1,contactid 如果仅仅需要返回子记录的ID,可以简单的加上 $ref 即可。 http://lycrmvm.cloudapp.net:5555/Demo/api/data/v8.0/accounts(CE23165A-3AA3-E511-80C7-000D3A807EC7)/contact_customer_accounts/$ref
function UTCToLocalTime(d) { var timeOffset = -((new Date()).getTimezoneOffset() / 60); d.setHours(d.getHours() + timeOffset); return d; } function ConvertStringToDatetime(dateString) { var reggie = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/; var dateArray = reggie.exec(dateString); var dateObject = new Date( (+dateArray[1]), (+dateArray[2]) - 1, (+dateArray[3]), (+dateArray[4]), (+dateArray[5]), (+dateArray[6]) ); return dateObject; } function NameOnchangeHandler() { var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("GET", encodeURI(clientURL + "/api/data/v8.0/ly_tests?$select=ly_name,ly_alternatekey,ly_bool,ly_datetime,ly_decimal,ly_float,_ly_lookup_value,ly_integer,ly_money,ly_multilinetext,ly_optionset,ly_singlelinetext&$orderby=ly_datetime asc&$filter=contains(ly_name,'Update消息测试')&$count=true"), true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function () { if (this.readyState == 4) { req.onreadystatechange = null; if (this.status == 200) { var responseJSON = JSON.parse(this.responseText); Xrm.Utility.alertDialog("本次查询返回符合条件的记录条数:" + responseJSON["@odata.count"]); if (responseJSON.value != null && responseJSON.value.length >= 1) { for (var i = 0; i < responseJSON.value.length; i++) { Xrm.Utility.alertDialog("ly_name=" + responseJSON.value[i].ly_name);//单行文本 Xrm.Utility.alertDialog("ly_alternatekey=" + responseJSON.value[i].ly_alternatekey);//单行文本 Xrm.Utility.alertDialog("ly_bool=" + responseJSON.value[i].ly_bool);//两个选项 Xrm.Utility.alertDialog("ly_datetime=" + UTCToLocalTime(ConvertStringToDatetime(responseJSON.value[i].ly_datetime)));//日期和时间,需要将UTC时间转换为本地时间 Xrm.Utility.alertDialog("ly_decimal=" + responseJSON.value[i].ly_decimal);//十进制数 Xrm.Utility.alertDialog("ly_float=" + responseJSON.value[i].ly_float);//浮点数 Xrm.Utility.alertDialog("ly_lookup=" + responseJSON.value[i]._ly_lookup_value);//查找字段,只有记录的主键Guid,且前面加下划线,后面加_value Xrm.Utility.alertDialog("ly_integer=" + responseJSON.value[i].ly_integer);//整数 Xrm.Utility.alertDialog("ly_money=" + responseJSON.value[i].ly_money);//货币 Xrm.Utility.alertDialog("ly_multilinetext=" + responseJSON.value[i].ly_multilinetext);//多行文本 Xrm.Utility.alertDialog("ly_optionset=" + responseJSON.value[i].ly_optionset);//选项集 Xrm.Utility.alertDialog("ly_singlelinetext=" + responseJSON.value[i].ly_singlelinetext);//单行文本 Xrm.Utility.alertDialog("主键ly_testid=" + responseJSON.value[i].ly_testid);//主键 Xrm.Utility.alertDialog("币种_transactioncurrencyid_value=" + responseJSON.value[i]._transactioncurrencyid_value);//币种 } } } else { var error = JSON.parse(this.responseText).error; Xrm.Utility.alertDialog("查询罗勇测试实体记录出错." + error.message); } } }; req.send(); }
下面例子是根据主键查询一条记录:
function UTCToLocalTime(d) { var timeOffset = -((new Date()).getTimezoneOffset() / 60); d.setHours(d.getHours() + timeOffset); return d; } function ConvertStringToDatetime(dateString) { var reggie = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/; var dateArray = reggie.exec(dateString); var dateObject = new Date( (+dateArray[1]), (+dateArray[2]) - 1, (+dateArray[3]), (+dateArray[4]), (+dateArray[5]), (+dateArray[6]) ); return dateObject; } function NameOnchangeHandler() { var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("GET", encodeURI(clientURL + "/api/data/v8.0/ly_tests(278FF7E9-22B4-E511-80DA-000D3A400088)?$select=ly_name,ly_alternatekey,ly_bool,ly_datetime,ly_decimal,ly_float,_ly_lookup_value,ly_integer,ly_money,ly_multilinetext,ly_optionset,ly_singlelinetext"), true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function () { if (this.readyState == 4) { req.onreadystatechange = null; if (this.status == 200) { var responseJSON = JSON.parse(this.responseText); Xrm.Utility.alertDialog("ly_name=" + responseJSON.ly_name);//单行文本 Xrm.Utility.alertDialog("ly_alternatekey=" + responseJSON.ly_alternatekey);//单行文本 Xrm.Utility.alertDialog("ly_bool=" + responseJSON.ly_bool);//两个选项 Xrm.Utility.alertDialog("ly_datetime=" + UTCToLocalTime(ConvertStringToDatetime(responseJSON.ly_datetime)));//日期和时间,需要将UTC时间转换为本地时间 Xrm.Utility.alertDialog("ly_decimal=" + responseJSON.ly_decimal);//十进制数 Xrm.Utility.alertDialog("ly_float=" + responseJSON.ly_float);//浮点数 Xrm.Utility.alertDialog("ly_lookup=" + responseJSON._ly_lookup_value);//查找字段,只有记录的主键Guid Xrm.Utility.alertDialog("ly_integer=" + responseJSON.ly_integer);//整数 Xrm.Utility.alertDialog("ly_money=" + responseJSON.ly_money);//货币 Xrm.Utility.alertDialog("ly_multilinetext=" + responseJSON.ly_multilinetext);//多行文本 Xrm.Utility.alertDialog("ly_optionset=" + responseJSON.ly_optionset);//选项集 Xrm.Utility.alertDialog("ly_singlelinetext=" + responseJSON.ly_singlelinetext);//单行文本 Xrm.Utility.alertDialog("主键ly_testid=" + responseJSON.ly_testid);//主键 Xrm.Utility.alertDialog("币种_transactioncurrencyid_value=" + responseJSON._transactioncurrencyid_value);//币种 } else { var error = JSON.parse(this.responseText).error; Xrm.Utility.alertDialog("查询罗勇测试实体记录出错." + error.message); } } }; req.send(); }
如果是查询实体的查找字段对应实体记录的某个字段值还有类似如下更快捷方法:
http://lycrmvm.cloudapp.net:5555/Demo/api/data/v8.0/accounts(CE23165A-3AA3-E511-80C7-000D3A807EC7)/primarycontactid?$select=telephone1 如果要查询子记录的话,可以使用如下的URL,其中的 contact_customer_account 是子实体的NavigationProperty的Partner属性的值。http://lycrmvm.cloudapp.net:5555/Demo/api/data/v8.0/accounts(CE23165A-3AA3-E511-80C7-000D3A807EC7)/contact_customer_accounts?$select=fullname,telephone1,contactid 如果仅仅需要返回子记录的ID,可以简单的加上 $ref 即可。 http://lycrmvm.cloudapp.net:5555/Demo/api/data/v8.0/accounts(CE23165A-3AA3-E511-80C7-000D3A807EC7)/contact_customer_accounts/$ref
标签:Xrm,Web,responseJSON,value,alertDialog,API,2015,Utility,ly 来源: https://www.cnblogs.com/luoyong0201/p/Dynamics_365_Web_API_Query_Data.html