Get Domain name for MS CRM 2011 User

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 !!!

One thought on “Get Domain name for MS CRM 2011 User

  1. Thomaz

    Hi !
    very useful, some copy paste errors
    var serverUrl = Xrm.Page.context.getServerUrl()
    -missing semi colon
    also char codes funny when cutpaste
    why not sourcelink

    overall cant you just:

    alert(Xrm.Page.context.getServerUrl());
    alert(document.domain);
    HTH

    Reply

Leave a Reply to Thomaz Cancel reply