Search code examples
dynamics-crmodatadynamics-crm-365

Cannot retrieve selected lookups using the OData endpoint


I'm unable to retrieve lookup fields when filtering an OData Request.

I used the following requests :

https://mycrm.api.crm4.dynamics.com/api/data/v9.1/contacts(guid)?$select=contactid,ownerid,createdby,new_expirefin,new_testcumul_stat

This request retrieves contactid, new_expirefin, and new_testcumul_stat, but no trace of ownerid and createdby.

In another hand, this request:

https://mycrm.api.crm4.dynamics.com/api/data/v9.1/contacts(guid)

return all fields, including those missing on the other request. Lookups are sent as Guid.

Both request uses the

Prefer = odata.include-annotations="*"

header. Knowing I cannot know which column are lookups (I'm working on a generic library), how could I retrieve those lookups ?


Solution

  • Using the format _lookupName_value allows you to retrieve the lookups:

    https://myOrg.api.crm.dynamics.com/api/data/v9.1/contacts(guid)?$select=contactid,fullname,_ownerid_value,_createdby_value

    Which of course leaves the problem of knowing which fields are lookups and thus need this formatting.

    This can help:
    https://myOrg.api.crm.dynamics.com/api/data/v9.1/EntityDefinitions(LogicalName='contact')?$select=LogicalName&$expand=ManyToOneRelationships($select=ReferencingAttribute,ReferencedEntity)