Monday, 6 March 2017

Web Api GET data using Java script MSCRM

// JavaScript source code


function WhoAmIRequest() {

    var qry = "accounts?$filter=startswith(name,'A') or startswith(name,'C')";
    var clientUrl = Xrm.Page.context.getClientUrl();

    var req = new XMLHttpRequest()

    req.open("GET", encodeURI(clientUrl + "/api/data/v8.0/" + qry  ), true);

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.onreadystatechange = function () {

        if (this.readyState == 4 /* complete */) {

            req.onreadystatechange = null;

            if (this.status == 200) {

                var data = JSON.parse(this.response);
               
                for(var i = 0; i < data.value.length ; i++ )
                {
                    alert("Acc Num Id : "+ data.value[i].accountnumber);
                }
                }

            else {

                var error = JSON.parse(this.response).error;

                alert(error.message);

            }

        }

    };

    req.send();

}