Thursday, June 11, 2020

Get CRM Contact list from Business Central using Odata Service

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:


        Now we will use the above and write the code:
       

    
    local procedure GetContactList(TokenText)
    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

Wednesday, June 10, 2020

CRM Odata Authentication from Business Central

Hello Readers,

I started working on CRM Odata Intergartion with Business Central and found it very difficult, there were no proper blog from where i could get the help/idea, i have spent so much time to get it working through AL solution.

Below is the working solution with the Odata integration with Business central, which will help you to make the solution ready with less effort.


In this part i will show you how to get the authentication token.

  1. GetOauthToken

        To get the authentication token below prerequisite are required.

a. Token URL  - below will be the URL, you can use common as well as your tenant id.
    https://login.microsoftonline.com/common/oauth2/token
    or
    https://login.microsoftonline.com/{tenant}/oauth2/token


b. Client Id - Get it from the app registration on azure.portal.com
         
   

c.  Client Secret - While registering the app you will get the secret id, copy it at the same time                                     because it get encrypted after refreshing the page.

  
d. Resource - Your CRM instance URL.

          e. Create Application user in CRM.
             
     
  : we will use the above information in our below code for authentication token
       
       
      procedure GetOauthToken()
      var
        TokenURL: Label 'https://login.microsoftonline.com/ad0cc5eb-XXXX-43d6-8496-c06723118d4f/oauth2/token';
        ClientId: Label '2eb0ae68-a0d9-43ac-8def-946151a8XXXX';
        ClientSecret: Label 'mA87Q/18XE:oU.e-=XXXXX[zXa7pG_80';
        Resource: Label 'https://xxTest.crm.dynamics.com';
        RequestBody: Label 'grant_type=client_credentials&client_id=%1&client_secret=%2&resource=%3';
        HttpClientHttpClient;
        RequestMessage: HttpRequestMessage;
        ResponseMessage: HttpResponseMessage;
        RequestHeader: HttpHeaders;
        Content: HttpContent;
        TempBlob: Record TempBlob;
        Outstr: OutStream;
        Instr: InStream;
        Nextbase: Text;
        Token: text;
        //Read Json
        JsonTokenJsonToken;
        JsonObjectJsonObject;
        JsonArrayJsonArray;
    begin

        Content.GetHeaders(RequestHeader);
        RequestHeader.Clear();

        HttpClient.SetBaseAddress(TokenURL);
        RequestMessage.Method('POST');
        RequestHeader.Remove('Content-Type');
        RequestHeader.Add('Content-Type''application/x-www-form-urlencoded');

        Clear(TempBlob);
        TempBlob.blob.CreateOutStream(Outstr);
        Outstr.WriteText(StrSubstNo(RequestBody, ClientId, ClientSecret, Resource));
        TempBlob.blob.CreateInStream(Instr);

        Content.WriteFrom(Instr);

        RequestMessage.Content(Content);
        HttpClient.Send(RequestMessage, ResponseMessage);

        if ResponseMessage.IsSuccessStatusCode() then begin
            ResponseMessage.Content().ReadAs(Nextbase);
        end;


        Clear(JsonObject);
        JsonToken.ReadFrom(Nextbase);
        JsonObject := JsonToken.AsObject;
        IF JsonObject.Get('access_token'JsonToken) then begin
            Token := JsonToken.AsValue().AsText();
        end;
        Message('Token: %1', Token);
    
    end;
       
     in next part we will see how to use the above token and get the list of contacts from the CRM Contact Odata web service.


Thank you,
Anish Agrawal
NAV / Business Central Developer



Get CRM Contact list from Business Central using Odata Service

Hello Readers, Hope you have gone through my last blog  i.e. CRM Odata Authentication with Business Central, lets continue with it, in this ...