locked
CRM 2011 - get logged user name javascript RRS feed

  • Question

  • Hi all, I used often in CRM 4.0 javascript which returned full user name of currently logged user when a form opens. In CRM 2011 it doesnt work, please could you help me with javascript which would work?

    And BTW do you have a javascript which returns any value from related entity? example: An Order has order line with Stock item, quantity and price. I would like (on change of the Stock item lookup) alert a values like measurements, weight etc from this stock item.

     

    Thank you Jan

    Monday, March 14, 2011 9:25 AM

Answers

  • Hi Tim, the code is exactly the same as yours, I just changed the filed name. (new_name in my case)

     

    Anyway, I made a little bit different solution which works. So if you have time you can play with this:

     

    function loggedUser() {
        //Get GUID of logged user
        var user = Xrm.Page.context.getUserId();
        var userId = user.substring(1,37);
        // Read the CRM Context to determine the CRM URL
        var serverUrl = Xrm.Page.context.getServerUrl()
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
        var ODATA_EntityCollection = "/SystemUserSet";
        // Specify the ODATA Query
        var ODATA_Query = "(guid\'" + userId + "')";
        // Combine into the final URL
        var ODATA_Final_url = serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection + ODATA_Query;

        //Calls the REST endpoint to retrieve data
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: ODATA_Final_url,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
               var userName = data.d.FullName;
               alert(userName);
               },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert('Error: '+ ODATA_Final_url);

                }
        });

    }

    Tuesday, March 29, 2011 7:20 AM

All replies

  • Hi unfortunately your links didnt help me too much, I preferre javascript solution, please dont you have some?
    Tuesday, March 22, 2011 8:14 AM
  • Hi,

    as user is stored in the crm database so you will need to query the data from crm.

    but if you really want to grab the logged user name, then you can get this through javascript from the html source of the webpage.

    If you open the html source of the webpage you will find the logged user name in the html source which is shown on the top right corner of the webpage in CRM.

    Run through the html source using javascript and try to dig out the logged user.

    hope it helps.

     


    cheers, S.Khan
    • Proposed as answer by Sanaullah Khan Wednesday, March 23, 2011 5:52 AM
    Wednesday, March 23, 2011 5:51 AM
  • Hi, thank you for your answer, but I was thinking more generally, in CRM 4.0 i have a javascript which returns any value from related entity (eg. order lines has stock item, on stock item is some attribute, i want to alert this value to user when creating a new order line and selecting the stock item - on change of the lookup field...) this javascript doesnt work in crm 2011 any more.

     

    Thursday, March 24, 2011 10:15 AM
  • Hi,

    You first need to convert your CRM 4 javascript to CRM 2011 javascript.

    use this tool to convert the javascript

    http://crm2011scriptconvert.codeplex.com/

    after that, put the converted javascript in the web resource files and call them appropriately.

    hope it helps.

     


    cheers, S.Khan
    Thursday, March 24, 2011 11:34 PM
  • Jan,

    The code below should be similar to what you need. In my case, I needed to place the current user's full name in a custom field when the form loaded.

    function SetFieldValueToCurrentUserName()
    {
     // Query for full name of the current user
     var curUserId = Xrm.Page.context.getUserId();
     var serverUrl = Xrm.Page.context.getServerUrl();
     var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
     var retrieveUserReq = new XMLHttpRequest();
     retrieveUserReq.open("GET", ODataPath + "/SystemUserSet(guid'" + curUserId + "')", false);
     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 = JSON.parse(retrieveUserReq.responseText).d;
         var userFullName = retrievedUser.FullName;
         var oField = Xrm.Page.data.entity.attributes.get("new_myfieldtopopulate");
         oField.setValue(userFullName);
         oField.setSubmitMode("always");
       }
     }
    }

    Regards,

    Tim Dutcher
    Altriva Solutions
    Bellevue, WA

    Friday, March 25, 2011 1:10 AM
  • Hi Tim, unfortunatelly the script doesnt work fully, i created a new web resource and put the script there, changed new_myfieldtopopulate to my filed schema name. I call both functions OnLoad of the form, I also added JSON and JQUERY.

    I put a few alerts and I get the ODataPath correctly and also retrieveUserReq.responseText correctly.

    I get an error readyState is null or not an object, if i coments this line I get another error status is null or not an object. If I comment both lines i get responseText is null or not an object. But the field is filled.

     

    Please could you help me?

     

    Thanx Jan

     

     

    Monday, March 28, 2011 1:05 PM
  • Hi Jan,

    Please post your code so I can help figure out where it's failing.

    Thanks,

    -Tim

    Monday, March 28, 2011 5:56 PM
  • Hi Tim, the code is exactly the same as yours, I just changed the filed name. (new_name in my case)

     

    Anyway, I made a little bit different solution which works. So if you have time you can play with this:

     

    function loggedUser() {
        //Get GUID of logged user
        var user = Xrm.Page.context.getUserId();
        var userId = user.substring(1,37);
        // Read the CRM Context to determine the CRM URL
        var serverUrl = Xrm.Page.context.getServerUrl()
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
        var ODATA_EntityCollection = "/SystemUserSet";
        // Specify the ODATA Query
        var ODATA_Query = "(guid\'" + userId + "')";
        // Combine into the final URL
        var ODATA_Final_url = serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection + ODATA_Query;

        //Calls the REST endpoint to retrieve data
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: ODATA_Final_url,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
               var userName = data.d.FullName;
               alert(userName);
               },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert('Error: '+ ODATA_Final_url);

                }
        });

    }

    Tuesday, March 29, 2011 7:20 AM
  • var retrievedUser = JSON.parse(retrieveUserReq.responseText).d;

    this line doesn't working for me. I get no reponse. Any ideas?


    Raza Usmani Software Engineer
    Friday, August 26, 2011 5:55 AM
  • what error are you getting ?
    cheers, S.Khan MCTS
    Friday, August 26, 2011 6:46 AM
  • Hi Raza, you can try my jscript from this thread...
    Monday, August 29, 2011 7:36 AM
  • Thanks Jan, I tried your script too. But I am not good with Jquery. when I tried to implement your code I received error message with $ sign undefined.

    Do we need to add additional items in order to use JQuery syntax?


    Raza Usmani Software Engineer
    Monday, August 29, 2011 8:04 AM
  • I do not get any message. No alerts. If I place a alert before this line it shows the message.

    Raza Usmani Software Engineer
    Monday, August 29, 2011 8:05 AM
  • Yes you need to add jquery1.4.1.min.js and json2.js libraries
    Monday, August 29, 2011 8:29 AM
  • I will also be thankful if you can provide me the steps to add the said items How and where? in CRM Dynamics 2011 on-premise.
    Raza Usmani Software Engineer
    Monday, August 29, 2011 9:01 AM
  • Please have a look at http://crmconsultancy.wordpress.com/2011/06/13/using-jquery-in-crm-2011/ it will guide you.

     

    Regards Jan

     

    • Proposed as answer by Raza Usmani Tuesday, August 7, 2012 6:31 AM
    Monday, August 29, 2011 9:11 AM
  • This has worked, Thanks for your help!


    Raza Usmani Software Engineer

    Tuesday, August 7, 2012 6:30 AM
  • Hello, I am trying to perform the similar functionality, but I am trying to launch this from a ribbon button on the Dashboard.

    I am trying to grab a custom field called "MatchingName" that is located on the User record and pass that field as a parameter into a Report Server report. From the ribbon button, I can have my alert show the GUID, but I cannot get it to display the Matching Name.

    NOTE: I replaced FullName in your code with MatchingName. When I put this code OnLoad in the User form, it works perfectly, but it does not work on the dashboard button.

    Here is my code:

    function Getinfo() {
        //Get GUID of logged user
        var user = Xrm.Page.context.getUserId(); 
        var userId = user.substring(1,37);
        // Read the CRM Context to determine the CRM URL
        var serverUrl = Xrm.Page.context.getServerUrl()
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
        var ODATA_EntityCollection = "/SystemUserSet";
        // Specify the ODATA Query
        var ODATA_Query = "(guid\'" + userId + "')";
        alert(userId);
        // Combine into the final URL
        var ODATA_Final_url = serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection + ODATA_Query;
        //Calls the REST endpoint to retrieve data
        $.ajax(
        {
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: ODATA_Final_url,
            beforeSend: function (XMLHttpRequest) 
            {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) 
            {
               var MatchingName = data.d.apd_MatchingID;
               alert(MatchingName);
               window.open("http://XXXXssrs/ReportServer/Pages/ReportViewer.aspx?%2fCRMXXXXXX_MSCRM%2fReports%2fDashboard+Reports%2fXXXXXX&XXXX=" + MatchingName);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) 
            {
                alert('Error: '+ ODATA_Final_url);
            }
            
        });
        
    }


    Mike Karls


    • Edited by Mike Karls Tuesday, December 18, 2012 7:04 PM syntax
    Tuesday, December 18, 2012 6:06 PM