By default you will get de codes and id's for optionsets and lookups. If you want to get the label as well, you need to add an extra header to the call.
$.ajax({
url: Xrm.Page.context.getClientUrl() + '/api/data/v8.0/contacts',
headers: {
'Accept': 'Application/json',
'Prefer': 'odata.include-annotations="OData.Community.Display.V1.FormattedValue"'
}
}).done(function (result) {
$.each(result.value, function (key, value) {
//sample to access a label
var gendercodeLabel = value['[email protected]'];
var gendercodeValue = value.gendercode;
});
});
For performance reasons you should minimize the number of fields you are requesting from the API. You can use the select property to do so.
This example fetches the name property of all accounts:
$.ajax({
url: Xrm.Page.context.getClientUrl() + '/api/data/v8.0/accounts?$select=name',
headers: {
'Accept': 'Application/json'
}
}).done(function (result) {
$.each(result.value, function (key, value) {
var lastname = value.primarycontactid.lastname;
});
});
If you fetch a single record and when that record has a lookup, you can also fetch values of the lookup value using the expand option. This reduces the number of calls you need to make to the API.
The sample gets all accounts and the last name of the primary contact:
$.ajax({
url: Xrm.Page.context.getClientUrl() + '/api/data/v8.0/accounts?$select=name,primarycontactid&$expand=primarycontactid($select=lastname)',
headers: {
'Accept': 'Application/json'
}
}).done(function (result) {
$.each(result.value, function (key, value) {
var lastname = value.primarycontactid.lastname;
});
});
This sample fetches accounts using a jQuery ajax method. On thing to note is that you need to set the header in the call to make the work.
$.ajax({
url: Xrm.Page.context.getClientUrl() + '/api/data/v8.0/accounts',
headers: {
'Accept': 'Application/json'
}
}).done(function (result) {
var accounts = result.value;
});
You can use the filter property to retrieve a subset of values from CRM. In this example only the accounts where the company name equals CompanyName are returned.
$.ajax({
url: Xrm.Page.context.getClientUrl() + '/api/data/v8.0/accounts?$filter=name eq CompanyName',
headers: {
'Accept': 'Application/json'
}
}).done(function (result) {
var accounts = result.value;
});