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

Dynamic CRM 2016使用WEB API 模拟用户查询记录(js)

2017-10-14 13:57 399 查看
本篇主要介绍了在Dynamic CRM WEB API中如何进行用户模拟查询数据

执行模拟的过程相当简单。在进行数据请求发送到Web服务,

在HTTP Request Header中追加MSCRMCallerID参数值等于模拟用户的systemuserid即GUID值。

对比代码如下:

//常规查询,权限为最高管理员权限
function RetrieveUsingWebAPIFetchxml() {
debugger;
var fetchxml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='account'>" +
"<attribute name='name' />" +
"<attribute name='primarycontactid' />" +
"<attribute name='telephone1' />" +
"<attribute name='accountid' />" +
"</entity>" +
"</fetch>";
var req = new XMLHttpRequest();
req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.0/accounts?fetchXml=" + encodeURI(fetchxml), 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 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response).value;
}
else {
var error = JSON.parse(this.response).error;
}
}
};
req.send();
impersonateRetrieveUsingWebAPIFetchxml();
}

//模拟查询,权限小于最高管理员权限
function impersonateRetrieveUsingWebAPIFetchxml() {
debugger;
var fetchxml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='account'>" +
"<attribute name='name' />" +
"<attribute name='primarycontactid' />" +
"<attribute name='telephone1' />" +
"<attribute name='accountid' />" +
"</entity>" +
"</fetch>";
var req = new XMLHttpRequest();
var impersonateUserId = "7AEBF217-91AF-E711-80C8-CD6AC961FE61";// UserGUID
req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.0/accounts?fetchXml=" + encodeURI(fetchxml), 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.setRequestHeader("MSCRMCallerID", impersonateUserId);
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 200) {
if (JSON.parse(this.response).value.length > 0)
var dataImpersonate = JSON.parse(this.response).value;
}
else {
var error = JSON.parse(this.response).error;
}
}
};
req.send();
}
RetrieveUsingWebAPIFetchxml查询结果如下:


impersonateRetrieveUsingWebAPIFetchxml 查询结果如下:

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