Reputation: 883
how to Write the javascript function for getting the username(not a owner) in crm 2011.
Upvotes: 2
Views: 6078
Reputation: 15128
If you mean the FullName
attribute of a SystemUser
record you can use this function:
function getCurrentUserFullName() {
var serverUrl;
if (Xrm.Page.context.getClientUrl !== undefined) {
serverUrl = Xrm.Page.context.getClientUrl();
} else {
serverUrl = Xrm.Page.context.getServerUrl();
}
var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var userRequest = new XMLHttpRequest();
userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false);
userRequest.setRequestHeader("Accept", "application/json");
userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
userRequest.send();
if (userRequest.status === 200) {
var retrievedUser = JSON.parse(userRequest.responseText).d;
var userFullName = retrievedUser.FullName;
return userFullName;
}
else {
return "error";
}
}
reference: CRM Answers - Get current user's full name with a synchronous call
Upvotes: 6
Reputation: 2170
Xrm.Page.context.getUserId();
This returns the GUID of the current user.
Query the User entity with the retrieved GUID to retrieve the name and any other details associated with the user.
Upvotes: 2