您的位置:首页 > Web前端 > JavaScript

Dynamic CRM 2016使用WEB API创建查询记录(js)

2017-10-09 14:52 375 查看
1.使用Post创建实体记录

代码如下

function CreateUsingWebAPI() {
var req = new XMLHttpreq()
var str = null;
debugger;
req.open("POST", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.0/accounts"), true);
req.setreqHeader("Accept", "application/json");
req.setreqHeader("Content-Type", "application/json; CHARSET=utf-8");
req.setreqHeader("OData-MaxVersion", "4.0");//可以省略此行
req.setreqHeader("OData-Version", "4.0");//可以省略此行
req.send(JSON.stringify({
name: "Sample account",//文本类型
new_twooptions: true,//bool类型
new_money1: 1000,//货币类型
new_optionset: 1000,//选项集
new_date: new Date(),//日期
"new_lookup@odata.bind": "/accounts(D5957650-59A5-E711-80C5-AFF8F414EE97)"//查找类型
}));
req.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status == 204) {
var accountUri = req.getResponseHeader("OData-EntityId");
alert("Created account with URI: " + accountUri);
}
else {
var error = JSON.parse(req.response).error;
alert(error.message);
}
}
};
}

创建成功,创建结果如图:



返回响应内容如下:

HTTP/1.1 204 No Content
OData-Version: 4.0
OData-EntityId: cc_WebAPI_ServiceURI/accounts(7eb682f1-ca75-e511-80d4-00155d2a68d1)
alert对应结果为:



2.使用Get查询实体记录

代码如下:

function RetrieveUsingWebAPI() {
var req = new XMLHttpRequest();
req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.0/accounts?$select=name&$top=2"), true);//查询account实体两条数据
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 data = JSON.parse(this.response).value;
}
else {
var error = JSON.parse(this.response).error;
}
}
};
req.send();
}查询信息如下图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: