Saturday, October 12, 2019

D365FO - Data integration by OData (Part 5 of 5)

D365FO - Data integration by OData (Part 1 of 5)
D365FO - Data integration by OData (Part 2 of 5)
D365FO - Data integration by OData (Part 3 of 5)
D365FO - Data integration by OData (Part 4 of 5)
D365FO - Data integration by OData (Part 5 of 5) You are here!


Create OData client application


Finally, here's the last of this series.


We will create OData client with AuthenticationUtility and ODataUtility we already made.



Create OData client

First, we create a C# console application project. Name it as 'TestODataClient'.



Next, add the following reference.
  • AuthenticationUtility
  • ODataUtility
  • Microsoft.OData.Client

Then, write the following code in Program.cs file.

























































 using AuthenticationUtility;  
 using ODataUtility.Microsoft.Dynamics.DataEntities;  
 using Microsoft.OData.Client;  
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Threading.Tasks;  
 namespace TestODataClient  
 {  
   class Program  
   {  
     public static string ODataEntityPath = ClientConfiguration.Default.UriString + "data";  
     public static void CreateMyCar(Resources context)  
     {  
       string CarID = "C0001";  
       DateTime todayDateTime = new DateTime(2019, 10, 11, 0, 0, 0);  
       DateTimeOffset todayDateTimeOffset = new DateTimeOffset(todayDateTime, new TimeSpan(+1, 0, 0));  
       decimal amount = 499999.99m;  
       try  
       {  
         MyCar MyCarEntity = new MyCar();  
         DataServiceCollection<MyCar> MyCarCollection = new DataServiceCollection<MyCar>(context);  
         MyCarCollection.Add(MyCarEntity);  
         MyCarEntity.CarID     = CarID;  
         MyCarEntity.BrandName   = "Suzuki";  
         MyCarEntity.SerieName   = "Swift";  
         MyCarEntity.Color     = "Black";  
         MyCarEntity.Price     = amount;  
         MyCarEntity.PurchasedDate = todayDateTimeOffset;  
         context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset);  
         Console.WriteLine(string.Format("My car record {0} - created !", CarID));  
       }  
       catch (DataServiceRequestException e)  
       {  
         Console.WriteLine(string.Format("My car record {0} - failed !", CarID));  
       }  
     }  
     static void Main(string[] args)  
     {  
       Uri oDataUri = new Uri(ODataEntityPath, UriKind.Absolute);  
       var context = new Resources(oDataUri);  
       context.SendingRequest2 += new EventHandler<SendingRequest2EventArgs>(  
         delegate (object sender, SendingRequest2EventArgs e)  
         {  
           var authenticationHeader = OAuthHelper.GetAuthenticationHeader();  
           e.RequestMessage.SetHeader(OAuthHelper.OAuthHeader, authenticationHeader);  
         });  
       CreateMyCar(context);  
       Console.ReadLine();  
     }  
   }  
 }  




Now, rebuild the project, run and if anything goes well, then get the following result.















Finally, check the update through the table and OData with the same way from the Part 1.

Table 







Finally, the data is inserted successfully from the external application into D365FO!

That's all!  Thanks for reading!




References
  • Rahul Mohta, Yogesh Kasat and JJ Yadav, Implementing MS Dynamics 365 for Finance and Operations, First published Sep 2017 (book)
  • Simon Buxton, Extending MS Dynamics 365 for Operations Cookbook, First published May 2017 (book)
  • Deepak Agarwal and Abhimanyu Singh, Dynamics 365 for Finance and Operations Development Cookbook, Fourth Edition Aug 2017 (book)
  • https://github.com/OData/odata.net/issues/1220

17 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Dear Shoot
    thanks for this amazing efforts
    now after I follow all steps I created first row from Odata finally
    But now i am confused
    how I will work with live environment .. I mean what should I do after visual studio project work well ?
    thanks again

    ReplyDelete
    Replies
    1. Hi Sherif,

      thanks for reading. I'm not sure I understand your question correctly. But the clue should be in the part I. So, OData allows the 3rd party application to connect/and consume D365FO data in an easy way (External --> AX).

      Delete
    2. Thanks for your replay Shoot
      I know this info from your scenario .. but here in part 5 we make a console application project to write code to push data .. in my 3rd party if it's with another programming language .. so the 3rd party need to write his code to push data and give him only dll that we created or I will make something else ?

      Delete
    3. Can I contact you via whatsapp if it's OK for you ?

      Delete
    4. Hi Sherif, you're correct. Console application just shows the sample of calling. When 3rd party app is written by other languages. We can apply the concept to that tool.

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hi Sir,

    i follow all steps but i m getting error "The type initializer for 'GeneratedEdmModel' threw an exception.", sorry i am asking silly question but i am new in Dynamics..

    ReplyDelete
    Replies
    1. Hi, can you check again part 4 of this article or check -> replace all "" (2 double quote) with " (single double quote)

      If not found, try to check the following various solutions.

      https://community.dynamics.com/365/financeandoperations/f/dynamics-365-for-finance-and-operations-forum/293972/odata-exception-the-type-initializer-for-generatededmmodel-threw-an-exception

      https://github.com/OData/odata.net/issues/1475

      Hope you can find some solution from them!

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hi shoot I am getting the below inner exception while running the console application. please help me what i am missing.

    I am using local onebox vm


    "AADSTS650052: The app needs access to a service (\\\"https://*.cloud.retail.dynamics.com\\\") that your organization \\\"idhanu.onmicrosoft.com\\\" has not subscribed to or enabled. Contact your IT Admin to review the configuration of your service subscriptions.\r\nTrace ID: 54b23a1e-4514-43bb-8e9e-3cea9d8c0500\r\nCorrelation ID: 53ed6fd1-cd7b-4a68-90f0-4125869d3806\r\nTimestamp: 2020-04-18 07:29:08Z"}

    ReplyDelete
  7. Dear sir i am having error on last step .on "MyCar" Can you please help me ?? MyCar MyCarEntity = new MyCar(); MyCar could not be found Error...

    ReplyDelete
    Replies
    1. Hi, MyCar was created in the 1st post of this series. You can check this variable again in post 1-4 of this series. Hope it helps.

      Delete
    2. Thanks for your response but it was Table with name of MyCar but in last part how we can access D365 Table in dotnet application MyCar was not identified in my case .

      Delete
  8. Beautifully explained! Iam from non coding background , still understood the logic in it.

    ReplyDelete
  9. What are the other ways of integration ... can you please explain

    ReplyDelete