locked
how to make web service request asynchronously in mscrm 2011 javascript RRS feed

  • Question

  • Dear all

    how to make web service request asynchronously in mscrm 2011 javascript

    Friday, June 21, 2013 9:05 PM

Answers

  • Or if you aren't looking to incorporate jQuery - hit the REST endpoint:

    function Contact_OnChange() {
        var contactid = "{Guid of contact}";
    
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/ContactSet?$select=MobilePhone&$filter=ContactId eq guid'" + contactid + "'";
    
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", oDataSelect, false); //false = sync & true = async
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        retrieveReq.onreadystatechange = function () {
            GetContactData(this);
        };
        retrieveReq.send();
    }
    
    function GetContactData(retrieveReq) {
        if (retrieveReq.readyState == 4) {
            if (retrieveReq.status == 200) {
                var retrieved = JSON.parse(retrieveReq.responseText).d;
                //Do something with returned data
            }
        }
    }
    


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, June 21, 2013 9:59 PM
    Moderator

All replies

  • Hi,

      you can always use jQuery $.ajax for that.. There are plenty of examples, here is one:

      http://technet.microsoft.com/en-us/library/gg328025.aspx


    Alex Shlega, GotchaHunter.Net

    Friday, June 21, 2013 9:54 PM
  • Or if you aren't looking to incorporate jQuery - hit the REST endpoint:

    function Contact_OnChange() {
        var contactid = "{Guid of contact}";
    
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/ContactSet?$select=MobilePhone&$filter=ContactId eq guid'" + contactid + "'";
    
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", oDataSelect, false); //false = sync & true = async
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        retrieveReq.onreadystatechange = function () {
            GetContactData(this);
        };
        retrieveReq.send();
    }
    
    function GetContactData(retrieveReq) {
        if (retrieveReq.readyState == 4) {
            if (retrieveReq.status == 200) {
                var retrieved = JSON.parse(retrieveReq.responseText).d;
                //Do something with returned data
            }
        }
    }
    


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, June 21, 2013 9:59 PM
    Moderator