// Define the soapBody for the WhoAmI request.
// Difference from Microsoft CRM 3.0 is requirement to call method GenerateAuthenticationHeader() in SOAP Envelope
var soapBody = "
soapBody += "
soapBody += "
soapBody += "
//Wrap the Soap Body in a soap:Envelope.
var soapXml = "
soapXml += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
soapXml += "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
soapXml += GenerateAuthenticationHeader();
soapXml += soapBody;
soapXml += "";
var SERVER_URL = "http://crm4.company.ru:5555";
// Create the XMLHTTP object for the execute method.
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");
//Send the XMLHTTP object.
xmlhttp.send(soapXml);
// Create an XML object to parse the results.
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlhttp.responseXML.xml);
// Get the user's ID.
var userid = xmlDoc.getElementsByTagName("UserId")[0].childNodes[0].nodeValue;
alert(userid);