web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Get Full Name of current user

(0) ShareShare
ReportReport
Posted on by 186

Hello

I found this msdn link with javascript functions: https://msdn.microsoft.com/en-us/library/gg334511.aspx

I am interested in getting easily the full name and the position of the current user.

Is this possible or do I have to use a function with oData or SOAP (something like this http://crmxpg.nl/wp/2010/05/18/return-multiple-fields-from-any-entity-via-ms-crm-webservices/)?

Best regards

*This post is locked for comments

I have the same question (0)
  • ozkary Profile Picture
    on at
    RE: Get Full Name of current user

    Yes, we can use the oData REST web service to query for the currently logged on  user. Read more here:

    www.ozkary.com/.../mscrm-get-user-information-with.html

  • Raphi Profile Picture
    186 on at
    RE: Get Full Name of current user

    Yes I got it at this moment now, thank you very much! My solution:

    function GetUserField(sField) {
       var 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; 
          if(sField == "fullname") {
             return retrievedUser.FullName;
          }
          else if(sField == "title") {
             return retrievedUser.Title;
          }
          else {
             return "Error! Field not found!";
          }
       }
       else {
          return "Error! Wrong status!";
       }
    }
    
    var sFullname = GetUserField("fullname");
    var sTitle = GetUserField("title");


  • Verified answer
    Royal King Profile Picture
    27,686 on at
    RE: Get Full Name of current user

    Try with below function

    function getUserInfo() {

       var req = new XMLHttpRequest();

       req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')?$select=FullName,Title"), false);

       req.setRequestHeader("Accept", "application/json");

       req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

       req.send();

       var result = JSON.parse(req.responseText).d;

       if (result.FullName !=null)

       var FullName = result.FullName;

       if (result.Title != null)

       var Title = result.Title;

    }

  • Raphi Profile Picture
    186 on at
    RE: Get Full Name of current user

    Thank you very much! It is exactly what I am looking for.

    But one problem:

    I cannot see how I can save the retrieved fields into variables, I tried to return the values, but I got "undefined" as an answer.

    I tried to do a return instead of alert..

    BR

  • Verified answer
    Community Member Profile Picture
    on at
    RE: Get Full Name of current user

    Hi,

    You need to use oData to get the current user's full name, try the function below:

    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) {

                   var UserGUID = Xrm.Page.context.getUserId();

                   var USerFullName = retrievedUser.FullName;

                   var UserTitle = retrievedUser.Title;

                  alert(USerFullName + " - " + UserTitle  );

               }

               //To check picklist

               if (retrievedUser.AccessMode != null)

               { }

           }

           else {

               alert("Error in Fetching User data");

           }

       }

    }

  • Mithilesh Kumar Profile Picture
    10,047 on at
    RE: Get Full Name of current user

    Hi Raphi,

    In that case you can do an oData Query to get your User and it's Position which is available within the User Record.

    Thanks

  • Raphi Profile Picture
    186 on at
    RE: Get Full Name of current user

    Hi

    I already got that, but I need also the position. It would be nice if there were some more functions in CRM..

    BR

  • Verified answer
    stuart Profile Picture
    2,080 on at
    RE: Get Full Name of current user

    Hi

    I use

    var userName=Xrm.Page.context.getUserName();

    for user name.

    Hope it helps

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans