1/30/14

MSCRM Get User Information with JavaScript

When working with Dynamics CRM forms, we can use the XRM client object model to query the fields and controls on the page. A common field on every form is the current user id. This however only provides the internal system id or GUID. When we need more information about the user, we can use the ODATA web services to get additional information.

This script provides us with a helper function to enable us to find additional information about the current logged on user.

if (typeof (OGBIT) == "undefined") { OGBIT = {}; }
OGBIT.UserInfo = {   
    Get: function () {

        var userInfo = {};
        if (self.userName == '') {
        var serverUrl = window.location.protocol + "//" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
           
            var userId = Xrm.Page.context.getUserId();
            var req = serverUrl + "/xrmservices/2011/OrganizationData.svc/SystemUserSet(guid'{0}')?$select=EmployeeId,FullName,InternalEMailAddress,SystemUserId,DomainName".replace('{0}', userId);
            try {
                $.ajax({
                    type: "GET", contentType: "application/json; charset=utf-8", datatype:"json",async:false,
                    url: req,
                    beforeSend: function (XMLHttpRequest) {
                        XMLHttpRequest.setRequestHeader("Accept", "application/json");
                    },
                    success: function (data, textStatus, XmlHttpRequest) {
                        userInfo = data.d;
                        userInfo.status = true; //dynamic property
                    },
                    error: function (XmlHttpRequest, textStatus, errorObject) {
                        userInfo.status = false;
                        //add error handling

                    }
                });
            } catch (e) { }
        }
        return userInfo;
    }
}

The script first calls the getUserId method of the page context. This returns the system id which we can then use to make an ODATA request to get the additional user information. The ODATA query to get the user information has this format:

SystemUserSet(guid'{0}')?$select=EmployeeId,FullName,InternalEMailAddress,SystemUserId,DomainName

The SystemUserSet allows us to query the System User entity.  We query for the current user with the guid’{0}’ parameter. The $select query option is used to indicate what additional fields should be returned. Those fields are listed below:

Parameters:
Field Name
Description
EmployeeId
employee id
FullName
First and last name
InternalEMailAddress
Email
SystemUserId
Internal ID or GUID
DomainName
Username with this format:
DomainName\Username

The following snippet can be used to test the script. With this function, we just call the Get method and assign the returning information into local variables.

function GetUserInfo() {
    if (typeof (OGBIT.UserInfo) != 'undefined') {
        var info = OGBIT.UserInfo.Get();
        if (info.status) {
            var empId = info.EmployeeId;
            var name = info.FullName;
            var email = info.InternalEMailAddress;
            var userName = info.DomainName;
            var id = info.SystemUserId;
        } else {
            alert('Unable to find user');
        }
    }
}


I hope this Script helps you get more understanding on how to interact with MS CRM.