Creating an ODataClient for MsDyn365FO
Paul Heisterkamp
Solution Architect at GWS mbH | former Microsoft MVP | FastTrack Recognized Solution Architect | DevOps Enthusiast | OneVersion and Evergreen ERP Advocate
I recently had the task to create an ODataClient for Microsoft Dynamics 365 for Finance and Operation. Because I faced some challenge, I want to share with you a step by step guide on how to create an ODataClient for MsDyn365FO.
Before I started my first try on the ODataClient, I found the following information:
- Official documentation on docs.microsoft.com: https://docs.microsoft.com/en-us/dynamics365/unified-operations/dev-itpro/data-entities/odata
- Dynamics-AX-Integration repository on GitHub:https://github.com/Microsoft/Dynamics-AX-Integration/tree/master/ServiceSamples/ODataConsoleApplication
- Tutorial from 2014 on how to create an ODataClient, which is missing some important steps: https://blogs.msdn.microsoft.com/odatateam/2014/03/11/tutorial-sample-how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class/
Before you begin, you have to install the Visual Studio extension "OData Client Code Generator". After the successful installation you will find the new item OData client in the "Add New Item" dialog. After adding such an item you have to open the *.tt file and set the TempFilePath to something like "C:/Tmp/metaTemp.xml" because the processing will have to deal with a big metadata file. Even though the comments in the template tells you that it is possible to use the metadata URL directly in the template, I can tell you that this does not work with MsDyn365FO, because the metadata XML is simply too big. You will see errors like "Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string literals."
Instead of directly using the URL of the metadata XML, I would save/download the metadata XML and use this file for the generation of the ODataClient. To do so open https://usnconeboxax1aos.cloud.onebox.dynamics.com/data/$metadata (https://<MsDyn365FO>/data/$metadata) and save the metadata XML with your browser. To use the saved file, change the value MetadataDocumentUri in the *.tt file to something like "File:///C:/Temp/metaProd.xml" and save it with Visual Studio. After a short processing time the ODataClient has been generated and is ready for building/compiling.
To use the created ODataClient you can use the samples from the Dynamics-AX-Integration repository e. g. authentication via OAuth.