dynamics-crm Using Web API with jQuery Using expand to get lookup properties


Example

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;
    });
});