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 !!
can you tell me how to use these functions?
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
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?
Hi Simon,
you just need to use AccessMode field name instead of FullName.
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?
yes because this is a picklist you need to write as alert(retrievedUser.AccessMode.Value);
Thanks Mahender, very helpful.
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!
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 :! :$
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.
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
Its now become easy with CRM 2013 to get the User Name of the logged in user. http://xrmadvice.wordpress.com/2014/08/08/crm-2013-get-logged-in-user-name-in-javascript/
Can you tell me the detailed explanation before exceting the code what the process should be done