如何在 Dynamics 365 中获取当前用户的业务部门

How to get current user's business unit in Dynamics 365

如何获取 javascript 登录用户的业务单位详细信息?我试过 Xrm.Utility.getGlobalContext().userSettings 但我无法获得业务部门的任何信息

如果你写 Xrm.Utility.getGlobalContext().userSettings 我假设你在 Model-driven 应用程序中使用 client-side javascript。

您可以从 userSettings 中获取 userId 属性,它 returns 当前用户的 GUID。

获得此值后,为了从用户的业务部门获取详细信息,您需要执行检索请求,如下所示:

// get the userId
var userId = Xrm.Utility.getGlobalContext().userSettings.userId;
// remove { and } from the userId
userId = userId.replace("{", "").replace("}", "");
// Xrm.WebApi call to retrieve details of the user (fullname)
// and the name of the businessunit (name from expand)
Xrm.WebApi.online.retrieveRecord("systemuser", 
userId,
"?$select=fullname&$expand=businessunitid($select=name)").then(
function success(result) {
    console.log(result);
    // Columns
    var systemuserid = result["systemuserid"]; // Guid
    var fullname = result["fullname"]; // Text
    
    // Many To One Relationships
    if (result.hasOwnProperty("businessunitid")) {
        var businessunitid_name = result["businessunitid"]["name"]; // Text
    }
},
function(error) {
    console.log(error.message);
}
);