Hello Readers,
Hope you have gone through my last blog i.e. CRM Odata Authentication with Business Central, lets continue with it, in this section we will see how to get the list of Contacts from CRM Odata service using the authentication bearer token which we got in the previous blog. CRM Odata Authentication with Business Central
Prerequisite required:
- Odata URL : Contact url exposed from CRM (ex. https://XXXXXX.api.crm.dynamics.com/api/data/v9.1/contacts).
- Authentication Token : follow CRM Odata Authentication with Business Central blog to get the authentication token
Now we will use the above and write the code:
local procedure GetContactList(Token: Text)
var
URL: Text;
Client: HttpClient;
Response: HttpResponseMessage;
Contacts: text;
begin
URL := 'https://XXXXXX.api.crm.dynamics.com/api/data/v9.1/contacts';
Client.DefaultRequestHeaders.Remove('Accept');
Client.DefaultRequestHeaders.Add('Accept', 'application/json');
Client.DefaultRequestHeaders.Add('OData-MaxVersion', '4.0');
Client.DefaultRequestHeaders.Add('OData-Version', '4.0');
Client.DefaultRequestHeaders.Add('Authorization', 'Bearer ' + Token);
if Client.Get(URL, Response) then
Response.Content().ReadAs(Contacts);
if Response.IsSuccessStatusCode() then
Message('Contacts: %1', Contacts);
end;
In next part we will see how to read the response and get contact data into NAV/BC table using Json Buffer.
Thank you,
Anish Agrawal
NAV / Business Central Developer