首页 > 解决方案 > 使用 Web API 进行用户模拟

问题描述

如何代表其他用户检索记录。

Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name&$top=3").then(
    function success(result) {
        for (var i = 0; i < result.entities.length; i++) {
            console.log(result.entities[i]);
        }                    
        // perform additional operations on retrieved records
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

标签: dynamics-crmdynamics-crm-365dynamics-crm-webapi

解决方案


我知道我们可以XMLHttpRequest通过传递MSCRMCallerID标题来进行模拟。不确定我们能否在Xrm.WebApi.

这是我的产品代码,在来自 HTML 网络资源的管理员模拟下执行一些更新/分配操作。

var entity = {};
entity["ownerid@odata.bind"] = "/systemusers(" + currentUserId + ")";

var req = new XMLHttpRequest();
req.open("PATCH", parent.Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.1/new_customentity(" + opptyid + ")", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("MSCRMCallerID", "0AFB2F7E-D323-E511-80F0-C4346BAC29F0"); //CRM Admin impersoantion
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 204) {
            //Success - No Return Data - Do Something
        } else {
            //Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send(JSON.stringify(entity));

更新

传递标头是唯一的方法,Xrm.WebApi不能接受请求标头。

文档说:

有两种方法可以模拟用户,这两种方法都可以通过传入带有相应用户 ID 的标头来实现。

Preferred:通过将该值与 header 一起传递,根据用户的 Azure Active Directory (AAD) 对象 ID 模拟用户CallerObjectId
Legacy:要根据用户的 systemuserid 模拟用户,您可以利用MSCRMCallerID相应的 guid 值。


推荐阅读