Tuesday, February 14, 2017

Sample JavaScript to retrieve using Web API in Dynamics CRM 2016 / 365

function executeWebApiQuery(query, type) {
    var result = null;
    var clientURL = Xrm.Page.context.getClientUrl();
    var req = new XMLHttpRequest()
    req.open(type, encodeURI(clientURL + query), false);
    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) {
            req.onreadystatechange = null;
            if (this.status == 200) {
                result = JSON.parse(this.response);

            }
            else {
                var error = JSON.parse(this.response).error;
                alert("Error : " + error.message);
            }
        }
    };
    req.send(null);
    return result;
}


var query = "/api/data/v8.1/businessunits(" + BUID + ")?$select=name";
var type = "GET";
var queryResult = executeWebApiQuery(query, type);
if (queryResult.Value.length > 0) {
    var BUName = queryResult.Value[0].name;
}

5 comments:

  1. Thanks for sharing such an informative post with us. I hope you will share more such post. Please keep sharing.
    HP Service center in Ameerpet, Hyderabad
    Best Laptop Service center in Ameerpet, Hyderabad

    ReplyDelete
  2. MDSU BCOM TimeTable 2020
    RU BCOM TimeTable 2020
    RDVV BCOM TimeTable 2020
    Thanks For Sharing!!! Wonderful blog & good post. It is really very helpful to me, waiting for a more new post. Keep Blogging

    ReplyDelete
  3. Between part them product avoid drug agency. Town help institution each even far. During war business star area million.religious

    ReplyDelete