Advertisements
Home > CRM 2016 > CRM 2016 Web API – Retrieve a record

CRM 2016 Web API – Retrieve a record

Below is the sample script to retrieve an Account record using Web API.

Key Points to perform Retrieve:

  • Use ‘GET’ request while performing Retrieve
  • ‘Status’ code would be 200.

Sample Script:

function retrieveAccounts() {
var clientURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest()
req.open(“GET”, encodeURI(clientURL + “/api/data/v8.0/accounts?$select=name&$top=1”), 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) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response, dateReviver);
if (data && data.value) {
for (var indxAccounts = 0; indxAccounts < data.value.length; indxAccounts++) {
var accountName = data.value[indxAccounts].name;
var eTag = data.value[indxAccounts][‘@odata.etag’];
}
}
}
else {
var error = JSON.parse(this.response).error;
alert(“Error retrieving Accounts – ” + error.message);
}
}
};

req.send(null);
}

function dateReviver(key, value) {
var a;
if (typeof value === ‘string’) {
a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
if (a) {
return new Date(Date.UTC(+a[1], +a[2] – 1, +a[3], +a[4], +a[5], +a[6]));
}
}
return value;
};

ETag:

  • Each time when we retrieve a record, it will include a @odata.etag field.

    Web API eTag

    Web API eTag

  • We don’t need to include it in a $select system query option.
  • The value of this property is updated each time the entity is updated.
  • This will be used while performing optimistic concurrency to detect whether an entity has been modified since it was last retrieved.

🙂

Advertisements
Categories: CRM 2016 Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: