Get Current User ID and Name in Javascript MS CRM 2011

If you want to get current user id in crm 2011 form you can use Xrm.Page.context, you have to use
getUserId() method for this you can use it like below

Xrm.Page.context.getUserId()

But if you want to get current user name then you can use below function to retrieve it

function Getinfo() {
var context;
var serverUrl;
var UserID;
var ODataPath;
context = Xrm.Page.context;
serverUrl = context.getServerUrl();
UserID = context.getUserId();
ODataPath = serverUrl + “/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 () {
retrieveUserReqCallBack(this);
};
retrieveUserReq.send();

}

function retrieveUserReqCallBack(retrieveUserReq) {
if (retrieveUserReq.readyState == 4 /* complete */) {
if (retrieveUserReq.status == 200) {
var retrievedUser = this.parent.JSON.parse(retrieveUserReq.responseText).d;
if (retrievedUser.FullName!= null)
alert(retrievedUser.FullName);

//To check picklist

if(retrievedUser.AccessMode!=null)

alert(retrievedUser.AccessMode.Value); // for picklist we need to check value
}
else {
alert(“Error in Fetching User data”);
}
}
}

Enjoy !!

13 thoughts on “Get Current User ID and Name in Javascript MS CRM 2011

    1. mahenderpal Post author

      you need to create webresource and need to call Getinfo, where you want to get data, for example if you want to get is onchange of some field attached webresource in ms crm form and call Getinfo function on change of particular field.

      let me know if you need any other information

      Reply
  1. Simon

    great post, it works well thanks! Any idea on how it can be modified to retrieve other values from picklist (optionset) fields on the SystemUserSet? For example, however can I retrieve the Access Mode value from the picklist that is on the User form?

    Reply
  2. Simon

    Thanks Mahender, that was the first thing I tried hoping it would be that easy however it falls back onto the error message. Maybe something to do with the fact AccessMode is a picklist field not just a straight text field?

    Reply
  3. check this out

    A thorough article… I’m astonished! Nearly all articles I’ve been browsing are total nonsense.
    I mean honestly, they aren’t even factually accurate. Thank goodness, your post is and I bookmarked it for later!

    Reply
  4. villerantala

    Nice, very Nice post, i understands some of this, one- This works with jQuery but look, i have a issue to do something like this, but what i need is when a user X user runs a workflow a field gets that user. Any help you can gimme :! :$

    Reply
  5. mahenderpal Post author

    What are you doing in workflow any modification ?? then you can simply use modifiedby field and set it to that field or you can write custom workflow to get it.

    Reply
  6. villerantala

    Thanks for the quickly answer Mahenderpal, look, i explain you a little more; I have the entity opportunities, wich is customizable just for the commercal user, i have a lookup field in this entity to select the developer user on charge of the opportinity this field is blank by default, so, the developer go to opportunities entity to check and etc, The developer just can runs as workflow to change the state of the opportunity, and i need to do that fill the look up field with the user who runs the workflow… but i don’t know how to do that :l

    Reply

Leave a Reply to Simon Cancel reply

Your email address will not be published. Required fields are marked *