26 January 2008

CRM 4.0 : get UserId, BusinessUnitId, OrganizationId on client-side JScript (WhoAmIRequest)

I'm sure it's in SDK, but to be clear I made it as a JScript function, so you can easly get UserId, BusinessUnitId and OrganisationId from client-side WhoAmIRequest, it's a good example.


function GetCurrentUserInfo()
{
var SERVER_URL = "http://CRM";
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 " +
"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'>";

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;

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

}

13 comments:

  1. Anonymous8:03 am

    How can I get the name of the the current systemuse to alert() the current systemuser in the above example using 4.0? Example: alert(systemuser)

    Thanks,

    Jer

    ReplyDelete
  2. Anonymous8:03 am

    How can I get the name of the the current systemuser to alert() the current systemuser in the above example using 4.0? Example: alert(systemuser)

    Thanks,

    Jer

    ReplyDelete
  3. Anonymous8:14 am

    How would you make the following code below grab the systemuser name using a Web Service and alert it using CRM 4.0?

    http://forums.microsoft.com/Dynamics/ShowPost.aspx?PostID=3362305&SiteID=27&mode=1


    I have reviewed your examples but I need a more direct answer. Thank you! Any help is appreciated!!

    ReplyDelete
  4. Anonymous3:36 am

    I discovered the solution! Thank you for all of your help!

    ReplyDelete
  5. Anonymous12:11 pm

    I test your example and its works perfect.
    I think u need to add some try and catch because if error returns your code doesn’t ménage exception
    take care
    Rami Heleg, Israel

    ReplyDelete
  6. Anonymous12:18 pm

    Hi Jim,
    i have a small question regarding diff between crm 3.0 and 4.0 JS requsts to crm services.
    in crm 3 we used remote command and now soap request.
    do u have any idea how faster the soap against preview options?
    thanks
    Rami Heleg.
    Israel

    ReplyDelete
  7. Thanks for the code!

    How do I change this so that is pulls from the owner of the record, not the person opening it?

    Also, how do I pull the Name of the owning user's business unit? So, instead of 'getElementsByTagName("BusinessUnitId")' something like 'getElementsByTagName("BusinessUnitIdName")'.
    I don't know where to find or how to display all of the Tag Names.

    Just so you know why I'm asking, I'm trying to display the owning user's business unit name (essentially, the account's or contact's business unit).


    P.S. - I modified the beginning of the code so it is more dynamic:

    function GetCurrentUserInfo()
    {
    var URL = SERVER_URL.replace('http://', 'http:||');
    if(URL.indexOf('/') >= 0)
    URL = URL.substring(0, URL.lastIndexOf('/'));
    URL = URL.replace('http:||', 'http://');
    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    xmlhttp.open("POST", URL + "/mscrmservices/2007/crmservice.asmx", false);
    ...

    ReplyDelete
  8. Hi All,

    How can I default/set “cc” field to the user’s account (email address) using email activity form based on the “onLoad” event?

    Please note once the form is loaded, and cc field is defaulted to user’s account, script must not restrict the user from adding multiple recipients as “cc” on this email form.

    Basically if you send emails using Web Client you dont end up having a copy of email sent from CRM in your Outloook. Just to ensure the CRM user doenst forget to add himself to the CC field, objective is therefore to include his or her user account as default onLoad form event.

    ReplyDelete
  9. I copied the function into my onload script for the form . Changed my server details eg http:/testserver/wptest and made a call to it GetCurrentUserInfo(); but got Error:- object required when I load the form. What step am I missing Jim?

    ReplyDelete
  10. Work it! I got userid in my Jscript to hide a tab. Thanks!

    ReplyDelete
  11. Great!!! Work it! I got userid in my jscript to hide a tab.
    Thanks!

    ReplyDelete
  12. I am trying to use a similar functionality to check all items in order products form and if one field (ex.graphic = Yes) update the salesorder form field (OrderType) picklist value to Graphic. I been having issues. I was going to create a workflow, but workflow will run everytime a item is added and update graphic. That is why if any item equals yes update ordertype on salesorder. Any suggestions?

    ReplyDelete