locked
Finding Currently Logged on User in MSCRM 4.0 RRS feed

  • Question

  •  

    Hi,

     

    We are working on MS CRM 4.0 (RTM). 

    We have a requirement wherein, on click of a custom button (created through ISV.CONFIG), I need to find the currently logged in user's userid and pass it as a querystring to one of the URL.

     

    I tried the following Javascript code, but it throws an error...

     

    //make a who am i request
    var whoAmIRequest = new RemoteCommand("SystemUser", "WhoAmI", "/MSCRMServices/");
    //execute the command and obtain a response
    var response = command.Execute();
    //get the user id from the response xml
    var userIdString = response.Xml.text;

    alert(userIdString);

     

    any help in this regard is very helpful and greatly appreciated.

     

    Thanks!!!

    Thursday, January 24, 2008 12:41 PM

All replies

  • Hi,

    Try this...
    function GetCurrentUserInfo()
    {

    var SERVER_URL = "http://crm:5555"; //replace with your crm server name

    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    xmlhttp.open("POST", SERVER_URL + "/mscrmservices/2007/crmservice.asmx", false);

    xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");


    var soapBody = "<soap:Body>"+

    "<Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+

    "<Request xsi:type='WhoAmIRequest' />"+

    "</Execute></soap:Body>";


    var soapXml = "<soap:Envelope " +

    "xmlnsTongue Tiedoap='http://schemas.xmlsoap.org/soap/envelope/' "+

    "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "+

    "xmlns:xsd='http://www.w3.org/2001/XMLSchema'>";

    soapXml += GenerateAuthenticationHeader();

    soapXml += soapBody;

    soapXml += "</soap:Envelope>";


    xmlhttp.send(soapXml);

    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

    xmlDoc.async=false;

    xmlDoc.loadXML(xmlhttp.responseXML.xml);

    var userid = xmlDoc.getElementsByTagName("UserId")[0].childNodes[0].nodeValue;

    //var buid = xmlDoc.getElementsByTagName("BusinessUnitId")[0].childNodes[0].nodeValue;

    //var orgid = xmlDoc.getElementsByTagName("OrganizationId")[0].childNodes[0].nodeValue;

    return userid;

    //alert("UserId: " + userid + "\r\nBusinessUnitId: " + buid + "\r\nOrganizationId: " + orgid);

    }
    Wednesday, March 26, 2008 6:28 AM
  • Hi Karthik,

    Is it possible to retrieve the user name using the WhoAmIRequest?

    if yes can you let me know how

    Thanks,
    Aarch
    • Proposed as answer by Aarch Tuesday, February 24, 2009 12:16 PM
    • Edited by Aarch Tuesday, February 24, 2009 12:18 PM
    Tuesday, February 24, 2009 6:48 AM
  • Hi,

    I got the solution from stunnware bog


    You can use the folowing code

    var xml = "" +

    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +

    GenerateAuthenticationHeader() +

    " <soap:Body>" +

    " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +

    " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +

    " <q1:EntityName>systemuser</q1:EntityName>" +

    " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +

    " <q1:Attributes>" +

    " <q1:Attribute>businessunitid</q1:Attribute>" +

    " <q1:Attribute>firstname</q1:Attribute>" +

    " <q1:Attribute>fullname</q1:Attribute>" +

    " <q1:Attribute>lastname</q1:Attribute>" +

    " <q1:Attribute>organizationid</q1:Attribute>" +

    " <q1:Attribute>systemuserid</q1:Attribute>" +

    " </q1:Attributes>" +

    " </q1:ColumnSet>" +

    " <q1:Distinct>false</q1:Distinct>" +

    " <q1:Criteria>" +

    " <q1:FilterOperator>And</q1:FilterOperator>" +

    " <q1:Conditions>" +

    " <q1:Condition>" +

    " <q1:AttributeName>systemuserid</q1:AttributeName>" +

    " <q1:Operator>EqualUserId</q1:Operator>" +

    " </q1:Condition>" +

    " </q1:Conditions>" +

    " </q1:Criteria>" +

    " </query>" +

    " </RetrieveMultiple>" +

    " </soap:Body>" +

    "</soap:Envelope>" +

    "";

     

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

     

    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

    xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");

    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

    xmlHttpRequest.send(xml);

     

    var resultXml = xmlHttpRequest.responseXML;

    var entityNode = resultXml.selectSingleNode("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");

     

    var firstNameNode = entityNode.selectSingleNode("q1:firstname");

    var lastNameNode = entityNode.selectSingleNode("q1:lastname");

    var fullNameNode = entityNode.selectSingleNode("q1:fullname");

    var systemUserIdNode = entityNode.selectSingleNode("q1:systemuserid");

    var businessUnitIdNode = entityNode.selectSingleNode("q1:businessunitid");

    var organizationIdNode = entityNode.selectSingleNode("q1:organizationid");

     

    //crmForm.all.sw_firstname.DataValue = (firstNameNode == null) ? null : firstNameNode.text;

    //crmForm.all.sw_lastname.DataValue = (lastNameNode == null) ? null : lastNameNode.text;

    var fullname = (fullNameNode == null) ? null : fullNameNode.text;

    • Proposed as answer by Aarch Tuesday, February 24, 2009 12:18 PM
    Tuesday, February 24, 2009 12:17 PM