An alternative way to retrieve, filter, create and modify CRM records is the OData-Endpoint. To access Dynamics 365 Data via REST you have to call the API:
Example: GET http://exampleserver:5555/Orgname/api/data/v8.2/rit_testentitys
As you can see you have to add an ‘s’ to the singular name of your entity. For non custom entitys this can be different. However, you can find the right name via the XrmToolbox under EntitySetName.
Alternatively you can retrieve the EntityDefinitions via OData to get the EntitySetName of any Entity. With the ‘select’-option you can determine the columns to retrieve. In this case ‘EntitySetName’ and ‘LogicalName’
GET http://exampleserver:5555/Orgname/api/data/v8.2/EntityDefinitions?$select=EntitySetName,LogicalName
To filter the query result, a ‘filter’-parameter is required http://exampleserver:5555/Orgname/api/data/v8.2/EntityDefinitions?$select=EntitySetName,LogicalName$filter=LogicalName eq 'opportunity'
In the response is now only one result.
Following filter operators are supported:
- Eq Equal
- Ne Not equal
- Gt Greater than
- Ge Greater than or equal
- Lt Less than
- Le Less than or equal
- And Logical and
- Or Logical or
To retrieve Lookup-Information like the FormattedValue and ID add an additional Header to the request: Prefer: odata.include-annotations="*"
With JavaScript, you can do it like this: $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url:"http://exampleserver:5555/Orgname/api/data/v8.2/rit_testentitys(5b9ec7f3-1a5e-46e2-9b8c-b2a5f866f5b9)",
beforeSend: function (request) { request.setRequestHeader("Accept", "application/json"); request.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); }, success: onSuccess, error: onError });
The response contains all lookup-information in separated attributes: _parentcustomerid_value@OData.Community.Display.V1.FormattedValue: "Testcompany", _parentcustomerid_value@Microsoft.Dynamics.CRM.associatednavigationproperty: "parentcustomerid_account", _parentcustomerid_value@Microsoft.Dynamics.CRM.lookuplogicalname: "account", _parentcustomerid_value: "61df8de6-2e11-46af-9b20-4a0345d124d1"
To fill a lookup field, you need the ID and EntitySetName of each Record: PATCH http://exampleserver:5555/Orgname/api/data/v8.2/rit_testentitys/(5b9ec7f3-1a5e-46e2-9b8c-b2a5f866f5b9)
Furthermore, an additional header is required. The X-HTTP-Method-Header specifies the data operation. A List of all available Methods is available at MSDN: https://msdn.microsoft.com/de-de/library/dd541471.aspx
In this case we need the “MERGE” value HEADER: "X-HTTP-Method", "MERGE" BODY: destinationLookupFieldsname@odata.bind=/contacts(82aa55d3-9914-430c-affd-4f207d811368)
To clear (disassociate) a lookup field, a delete request is required.
Use the $ref keyword to edit the reference
DELETE http://exampleserver:5555/Orgname/api/data/v8.2/rit_testentitys/(5b9ec7f3-1a5e-46e2-9b8c-b2a5f866f5b9)/contacts(82aa55d3-9914-430c-affd-4f207d811368)/$ref