Wednesday, August 1, 2018

How to Consume JSON Web API using GET Method in Dynamics 365 FO


One very basic requirement now a days is the consumption of data of  any other Web Based Application in Dynamics 365 Finance and Operations.

Similary we can use the code below for consumption of a JSON based Web API used to expose data from Microsoft Dynamics CRM but instead it can be used for any other kind of Web Application hosted as a Web API on Azure.



        int                                                      find;
        str                                                      url,aosUri,activeDirectoryTenant;
        str                                                      activeDirectoryClientAppId;
        str                                                      activeDirectoryClientAppSecret;
        str                                                      postData,activeDirectoryResource,
        str                                                      aadClientAppSecret,oAuthHeader;
        str                                                      returnValue,jsonString,jsondszstr;
        System.Net.HttpWebRequest           request;
        System.Net.HttpWebResponse         response;
        System.Byte[]                                   byteArray;
        System.IO.Stream                             dataStream;
        System.IO.StreamReader                 streamRead;
        System.IO.StreamWriter                  streamWrite;
        System.Net.ServicePoint                  servicePoint;
        System.Net.ServicePointManager    servicePointmgr;
        System.Net.HttpVersion                   version;
        CLRObject                                        clrObj;
        Newtonsoft.Json.JsonReader            reader;
        System.Text.Encoding                      utf8;
        Counter                                             countCounter;
        Object                                               obj;
        Map                                                  data;

        System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();
   

        new InteropPermission(InteropKind::ClrInterop).assert();

     

        headers = new System.Net.WebHeaderCollection();
        url = "https:///abcd1234.crm4.dynamics.com/api/data/v8.1/detail";
     
        clrObj = System.Net.WebRequest::Create(url);
     
     
        request = clrObj;
        request.set_Method("GET");
        request.set_KeepAlive(true);
     
        request.set_ContentType("application/json");

        aosUri = "https://login.windows.net/xyz1234/oauth2/authorize?";

        activeDirectoryTenant = "https://login.microsoftonline.com/xyz1234/oauth2/token";

        activeDirectoryClientAppId = "abcd3-xyz456-7yd7-09a1-09865ec1d3";

        activeDirectoryResource = "https://abcd1234.crm4.dynamics.com";

        Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authenticationContext =      new           Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(activeDirectoryTenant);

     

        var userCredential = new  Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential("admin@xyz.onmicrosoft.com", "abcd@1234");

        Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult authenticationResult =
            authenticationContext.AcquireTokenAsync(activeDirectoryResource, activeDirectoryClientAppId, userCredential).Result;

     
        headers.Add("Authorization", authenticationResult.CreateAuthorizationHeader());
        request.set_Headers(headers);
     
     

        servicePoint = request.get_ServicePoint();

        System.Net.ServicePointManager::set_Expect100Continue(false);

        System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Tls12);

     

        response = request.GetResponse();

     
        dataStream = response.GetResponseStream();


        streamRead = new System.IO.StreamReader(dataStream);

        jsonString = streamRead.ReadToEnd();

        info(strFmt("%1",jsonString));

        dataStream.Close();

        response.Close();
   

How to reverse Free Text Invoice Voucher entries without Dialog

 Hey Folks ,  This blog post is in continuation of the previous post for Reversing Free Text Invoice Voucher entries with Dialog. Only diffe...