示例调用代码如下:
var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("GET", encodeURI(clientURL + "/api/data/v8.0/systemusers(4C6A6247-43A9-E511-80CF-000D3A806074)/Microsoft.Dynamics.CRM.RetrieveAllChildUsersSystemUser()?$select=fullname"), 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 responseJSON = JSON.parse(this.responseText); var popstr = ""; if (responseJSON.value != null && responseJSON.value.length >= 1) { for (var i = 0; i < responseJSON.value.length; i++) { popstr += responseJSON.value[i].fullname + ";"; } } Xrm.Utility.alertDialog("这个人下属(含直接下属和非直接下属)包括:" + popstr); } else { var error = JSON.parse(this.response).error; Xrm.Utility.alertDialog("调用函数出错." + error.message); } } }; req.send();
非绑定函数(unbound function)就是不是和实体绑定的函数,所有的查询函数都是非绑定函数,还有部分函数,他们没有IsBound属性。
下图是非绑定函数 WhoAmI 定义的元数据。
下面是调用非绑定函数的示例代码:
var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("GET", encodeURI(clientURL + "/api/data/v8.0/WhoAmI()"), 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 responseJSON = JSON.parse(this.responseText); Xrm.Utility.alertDialog("当前用户的ID是:" + responseJSON.UserId + ";所属业务部门ID是" + responseJSON.BusinessUnitId); } else { var error = JSON.parse(this.response).error; Xrm.Utility.alertDialog("调用函数出错." + error.message); } } }; req.send();更多信息请参考官方文档: Use Web API functions 。