Sometime we have requirement to get domain name for crm user, we can
use SystemUser entity to get this information, you can use below rest
code for the same:
function GetServerURL()
{
var serverUrl = Xrm.Page.context.getServerUrl();
if (serverUrl.match(/\/$/)) {
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
return serverUrl;
}
function GetDomainName() {
var UserID = Xrm.Page.context.getUserId();
var ODataPath = GetServerURL() + "XRMServices/2011/OrganizationData.svc";
var retrieveUserReq = new XMLHttpRequest();
retrieveUserReq.open("GET", ODataPath + "/SystemUserSet(guid'" + UserID + "')", true);
retrieveUserReq.setRequestHeader("Accept", "application/json");
retrieveUserReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveUserReq.onreadystatechange = function () {
GetDomainNameCallBack(this);
};
retrieveUserReq.send();
}
function GetDomainNameCallBack(retrieveUserReq) {
if (retrieveUserReq.readyState == 4 /* complete */) {
if (retrieveUserReq.status == 200) {
var retrievedUser = this.parent.JSON.parse(retrieveUserReq.responseText).d;
if(retrievedUser.DomainName!=null)
alert(retrievedUser.DomainName);
}
else {
alert("Error in Fetching User data");
}
}
}Hope it will help someone !!!