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

Advertisements

1 Comment

Filed under MS CRM 2011

One response to “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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s