Tag: Web api

WEB API- Part 5 : Get lookup text

Get lookup text using WEB API

Following code will fetch the originating lead lookup text and Id from an opportunity .

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/opportunities?$select=_originatingleadid_value&$filter=_originatingleadid_value ne null", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                 var _originatingleadid_value = results.value[i]["_originatingleadid_value"];// Id
                 var _originatingleadid_value_formatted = results.value[i]["_originatingleadid_value@OData.Community.Display.V1.FormattedValue"];// text value
            }
        }
        else {
            alert(this.statusText);
        }
    }
};
req.send();

WEB API- Part 4 : Get option-set text

Get option-set text and value using Web API

Following code fetches lead source option-set value and text from lead entity.

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/leads?$select=firstname,leadsourcecode&$filter=fullname eq 'Alex%20Eric'", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                 var firstname = results.value[i]["firstname"];
                 var leadsourcecode = results.value[i]["leadsourcecode"];
                 var leadsourcecode_formatted = results.value[i]["leadsourcecode@OData.Community.Display.V1.FormattedValue"];
            }
        }
        else {
            alert(this.statusText);
        }
    }
};
req.send();

Please note the following code

" var leadsourcecode_formatted = results.value[i]["leadsourcecode@OData.Community.Display.V1.FormattedValue"];"

“fields name@OData.Community.Display.V1.FormattedValue” will give you the text value of the filed.

Note : Don’t forget to add req.setRequestHeader(“Prefer”, “odata.include-annotations=\”OData.Community.Display.V1.FormattedValue\””);
in the request header

WEB API- Part 2: Retrieve Single Record Using WEB API

Retrieve Single Record Using WEB API

Following  code shows how to fetch an account record using  Guid

var req = new XMLHttpRequest();

var AcccountID="257bAC5D5240-8B31-E611-80E4-5065F38B4681";
req.open("GET", Xrm.Page.context.getClientUrl() +"/api/data/v8.0/accounts(AcccountID)?$select=name", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var name = result["name"];
}
else {
alert(this.statusText);
}
}
};
req.send();

Let’s Play with WEB API – Part -1 : Retrieve Multiple Records

Retrieve Multiple  Records using Web API

The Web API is a newly introduced open standard endpoint currently available in CRM 2016  at [organization uri]/api/data/v8.0/ which implements ODATA v4 and allows you the full blown functionality that you would typically experience when interacting through the CRM Organisation Service.

The Web API service is said to eventually replace the existing Organisation and Organisation Data service and MS recommend that if you writing new code that you begin using the Web API endpoint as the existing ODATA REST endpoint is officially deprecated but still available for backward compatibility.

The new API has introduced some impressive functionality that was not available to us previously with the existing ODATA service. Some of that functionality includes:

  • Ability to execute saved queries
  • Ability to execute FetchXML
  • Use Queries Up to Second Level

Following code shows how to retrieve records using web API, I have tested to retrieve 2000+ records and it is working fine.

This code alerts the number of contacts under an Account entity.

function AccountOnLoad()

{

    var Id = Xrm.Page.data.entity.getId().substring(1, 37);

    var entity = "contacts";

 var columnSet = "?$select=firstname&$filter=_accountid_value eq " + Id + "&$count=true";

    var options = columnSet;

  

    var serverURL = Xrm.Page.context.getClientUrl();

    var Query = entity + options;

    var req = new XMLHttpRequest();

    req.open("GET", serverURL + "/api/data/v8.0/" + Query, 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);

                if (data['@odata.count'] != null)

                    alert("This Account Has" + data['@odata.count'] + "Contacts");

            } else {

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

                alert(error.message);

            }

        }

    };

    req.send();

}

 

There are many other features too which I will be demonstrating in the upcoming posts.