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

About these ads

12 Comments

Filed under MS CRM 2011

12 responses to “Get Current User ID and Name in Javascript MS CRM 2011

  1. Raza Usmani

    can you tell me how to use these functions?

    • mahenderpal

      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

  2. 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?

  3. 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?

  4. Simon

    Thanks Mahender, very helpful.

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

  6. 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 :! :$

  7. mahenderpal

    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.

  8. 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

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