locked
Get the Current User ID and FullName JAVASCRIPT RRS feed

  • Question

  • I can't seem to find any examples of just getting the current User ID and Full Name.
    Here's my issue so maybe there is another way.  I have bit field called new_approved that a manager can approve a quote and then activate it.  Well I need to record what user did this, so I have created 2 fields called new_approveduser and new_approveddate.  Well normally I use a workflow in situation like this and just use the modified user when new_approved is changed.  However in quotes once a quote has been activated it can't be changed via a WF.

    Thanks!!!
    Withers
    Monday, May 4, 2009 5:02 PM

Answers

All replies

  • Hi.

    You can use following code to retrieve username and userid trought java script:
    var oXmlHttp = new ActiveXObject(“MSXML2.XMLHTTP”);
    oXmlHttp.open(“POST”, “/mscrmservices/2007/crmservice.asmx”, false);
    oXmlHttp.setRequestHeader(“Content-Type”, “text/xml”);
    oXmlHttp.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/crm/2007/WebServices/Execute”);
    
    var Env = “”;
    Env += “<soap:Envelope xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance’ “;
    Env += “xmlns:xsd=’http://www.w3.org/2001/XMLSchema’ xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/’>”;
    Env += GenerateAuthenticationHeader();
    Env += “<soap:Body>”;
    Env += “<Execute xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>”;
    Env += “<Request xsi:type=’WhoAmIRequest’ />”;
    Env += “</Execute>”;
    Env += “</soap:Body>”;
    Env += “</soap:Envelope>”;
    
    oXmlHttp.send(Env);
    
    var xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);
    xmlDoc.async = false;
    xmlDoc.loadXML(oXmlHttp.responseXML.xml);
    var userId = xmlDoc.getElementsByTagName(‘UserId’)[0].childNodes[0].nodeValue;
    var userName = xmlDoc.getElementsByTagName(‘UserId’)[0].childNodes[0].getAttribute('name');


    Истина открывается подготовленному уму. Мой блог - http://a33ik.blogspot.com
    Monday, May 4, 2009 6:26 PM
    Moderator
  • Hi.

    You can use following code to retrieve username and userid trought java script:
    var oXmlHttp = new ActiveXObject(“MSXML2.XMLHTTP”);
    oXmlHttp.open(“POST”, “/mscrmservices/2007/crmservice.asmx”, false);
    oXmlHttp.setRequestHeader(“Content-Type”, “text/xml”);
    oXmlHttp.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/crm/2007/WebServices/Execute”);
    
    var Env = “”;
    Env += “<soap:Envelope xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance’ “;
    Env += “xmlns:xsd=’http://www.w3.org/2001/XMLSchema’ xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/’>”;
    Env += GenerateAuthenticationHeader();
    Env += “<soap:Body>”;
    Env += “<Execute xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>”;
    Env += “<Request xsi:type=’WhoAmIRequest’ />”;
    Env += “</Execute>”;
    Env += “</soap:Body>”;
    Env += “</soap:Envelope>”;
    
    oXmlHttp.send(Env);
    
    var xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);
    xmlDoc.async = false;
    xmlDoc.loadXML(oXmlHttp.responseXML.xml);
    var userId = xmlDoc.getElementsByTagName(‘UserId’)[0].childNodes[0].nodeValue;
    var userName = xmlDoc.getElementsByTagName(‘UserId’)[0].childNodes[0].getAttribute('name');


    Истина открывается подготовленному уму. Мой блог - http://a33ik.blogspot.com

    well I changed your ticks and double ticks to be single quote and double quotes as VS 2008 said invalid character.  Still isn't working.

    Withers
    Monday, May 4, 2009 7:41 PM
  • Hi.

    What kind of project do you develop now? Plugin, custom workflow or custom web page?
    Истина открывается подготовленному уму. Мой блог - http://a33ik.blogspot.com
    Monday, May 4, 2009 7:43 PM
    Moderator
  • I commented out this  and it works fine
    var userName = xmlDoc.getElementsByTagName(‘UserId’)[0].childNodes[0].getAttribute('name');
    minus the fact that I don't have the full name

    Withers
    Monday, May 4, 2009 7:45 PM
  • I use VS 2008 to write my onLoad events, the CRM windows are just too small.  then copy and paste them back in.  This also allows for better formatting.




    Withers
    Monday, May 4, 2009 7:48 PM
  • Hi.

    It's my mistake - here the article about this request. The responce doesn't contain any name.

    Add following code to the tail of existing:

    var authenticationHeader = GenerateAuthenticationHeader();
    // Prepare the SOAP message.
    var xml = “”;
    xml = 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’>”+
    authenticationHeader+
    “<soap:Body>”+
    “<Retrieve xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>”+
    “<entityName>systemuser</entityName>”+
    “<id>”+userid+”</id>”+
    “<columnSet xmlns:q1=’http://schemas.microsoft.com/crm/2006/Query’ xsi:type=’q1:ColumnSet’>”+
    “<q1:Attributes>”+
    “<q1:Attribute>”+fullname+”</q1:Attribute>”+
    “</q1:Attributes>”+
    “</columnSet>”+
    “</Retrieve>”+
    “</soap:Body>”+
    “</soap:Envelope>”;
    // Prepare the xmlHttpObject and send the request.
    var xHReq = new ActiveXObject(”Msxml2.XMLHTTP”);
    xHReq.Open(”POST”, “/mscrmservices/2007/CrmService.asmx”, false);
    xHReq.setRequestHeader(”SOAPAction”,”http://schemas.microsoft.com/crm/2007/WebServices/Retrieve”);
    xHReq.setRequestHeader(”Content-Type”, “text/xml; charset=utf-8″);
    xHReq.setRequestHeader(”Content-Length”, xml.length);
    xHReq.send(xml);
    // Capture the result.
    var resultXml = xHReq.responseXML;
    var userName = resultXml.selectSingleNode("//q1:fullname").nodeTypedValue;
    I don't remember what is main attribute for user entity. Code was written for field fullname. If it isn't primary field - replace fullname with coorect field.
    Истина открывается подготовленному уму. Мой блог - http://a33ik.blogspot.com
    Monday, May 4, 2009 8:03 PM
    Moderator
  • This is what I have and if I leave the first fullname, it says undefined (pretty sure it thinks its a variable).  So I removed the quotes and plus sign so that just fullname was between the attribute nodes.  When I do that it asks me to log onto the server 3 times then says I have an invalid object.

    //Get the User ID and Full Name
    //
    var CurrentUserId;
    var CurrentUserName;
    function GetCurrentUserInfo()
    {
    var oXmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
    oXmlHttp.open("POST", "/mscrmservices/2007/crmservice.asmx", false);
    oXmlHttp.setRequestHeader("Content-Type", "text/xml");
    oXmlHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");

    var Env = "";
    Env += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
    Env += "xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
    Env += GenerateAuthenticationHeader();
    Env += "<soap:Body>";
    Env += "<Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>";
    Env += "<Request xsi:type='WhoAmIRequest' />";
    Env += "</Execute>";
    Env += "</soap:Body>";
    Env += "</soap:Envelope>";

    oXmlHttp.send(Env);

    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.loadXML(oXmlHttp.responseXML.xml);
    CurrentUserId = xmlDoc.getElementsByTagName('UserId')[0].childNodes[0].nodeValue;

    var authenticationHeader = GenerateAuthenticationHeader();
    // Prepare the SOAP message.
    var xml = "";
    xml = 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'>"+
    authenticationHeader+
    "<soap:Body>"+
    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
    "<entityName>systemuser</entityName>"+
    "<id>"+CurrentUserId+"</id>"+
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>" + fullname + "</q1:Attribute>"+
    "</q1:Attributes>"+
    "</columnSet>"+
    "</Retrieve>"+
    "</soap:Body>"+
    "</soap:Envelope>";
    // Prepare the xmlHttpObject and send the request.
    var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
    xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xHReq.setRequestHeader("Content-Length", xml.length);
    xHReq.send(xml);
    // Capture the result.
    var resultXml = xHReq.responseXML;
    CurrentUserName = resultXml.selectSingleNode("//q1:fullname").nodeTypedValue;

    }
    GetCurrentUserInfo();
    Withers
    Tuesday, May 5, 2009 12:10 AM
  • also for some reason you are pasting unicode. 

    Withers
    Tuesday, May 5, 2009 12:38 AM
  • Hi,

    You could try this code as well


    Retrieving the current user information
    http://www.stunnware.com/crm2/topic.aspx?id=JS31

    Works perfectly !
    (One of the best site for CRM developers)

    Regards,
    Nishant Rana

    http://nishantrana.wordpress.com
    Tuesday, May 5, 2009 5:16 AM
  • hi

    To get current user id
    Xrm.Page.getAttribute(“ownerid”).getValue()[0].id

    and for user name
    Xrm.Page.getAttribute(“ownerid”).getValue()[0].name

    Wednesday, December 26, 2012 2:09 PM