Accessing Dynamics 365 REST Endpoint via OData | R.iT News-Blog

R.iT GmbH
2 min readJun 1, 2021

--

31.01.2019 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 exampleserver/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

Originally published at https://www.rit.de.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

R.iT GmbH
R.iT GmbH

Written by R.iT GmbH

Als iT-Unternehmensberatung für die Digitale Transformation begleitet die R.iT GmbH seit mehr als 20 Jahren Mittelstandsunternehmen am Markt.

No responses yet

Write a response