After attending Tech Conference this year in Seattle, I decided to try consuming Dynamics 365 for Finance and Operations data entities through OData services from a .NET application (it is about time…). As you may know, this is replacing the old Dynamics AX 2012 AIF document services (AXDs).
I found few links on the Internet but it was not that straight forward to configure/implement so I thought I would just share the steps here 🙂
Azure app configuration
- Sign in to the Azure Portal with your O365 account which has access to D365 for Finance and Operations.
- Select Azure Active Directory icon on the left-hand side and App registrations then click New application registration.
- Enter your app name, select application type Native, enter your organization’s URL in Redirect URI (e.g. https://demoXXXaos.cloudax.dynamics.com) and then click Create.
- In Search by name or AppId field, enter your app name (or part of it) so it is filtering the list of apps and select it from the list.
- Now let’s create a new key for the registered app. Enter a Key description, Duration and click Save. It will automatically generate a key value that you will need to save so that we can use it later in the Visual Studio project. Also save the Application ID for later.
- Next you will need to add required permissions for Dynamics 365 for Financials.
- Tick the Access as the signed-in user delegated permission and click Select.
- Verify that you can see the Dynamics 365 for Financials delegated permission.
D365 for Finance and Operations configuration
- Open your Dynamics 365 for Finance and Operations in your preferred web browser.
- In System administration > Setup > Azure Active Directory applications, click the New button to create a new record.
- Enter your Azure Application Id into the Client Id field.
- Enter your Azure Application name into the name field.
- Select Admin in User ID field.
- Click the Save button.
OData client configuration
- Download the codebase from Microsoft in GitHub here.
- Start a new instance of Visual Studio 2015 and open the ServiceSamples solution.
- As suggested in the first link in References, I could not update the MetadataDocumentUri variable in ODataClient.tt file and save it to re-generate the ODataClient.cs.
- Instead I had to delete the ODataClient.tt and ODataClient.ttinclude and re-create the OData client.
- Right click on ODataUtility project > Add > New item, search for ‘OData’ in Online and rename it to ODataClient.tt (if you already installed it will appear under Installed on the left-hand side).
- In Solution Explorer, open ODataClient.tt file that you just added and update the MetadataDocumentUri variable to “/data/” where is your organization’s URL, for example “https://demoXXXaos.cloudax.dynamics.com/data/”.
- Then save it to generate the proxy classes (might take a few min).
- You should be able to see proxy classes in the ODataClient.cs.
- Still in the the ServiceSamples solution in Visual Studio 2015, open the ClientConfiguration.cs under the AuthenticationUtility project.
- Update all variables under OneBox:
- UriString with your organization’s URL
- Username with you O365 account with access to Dynamics 365 for Finance and Operations
- Password with your password
- ActiveDirectoryTenant, same as your organization’s URL without the “/” at the end
- ActiveDirectoryTenant with your tenant Id
- ActiveDirectoryClientAppId with your Azure application Id
- ActiveDirectoryClientAppSecret with you Azure key value
- Save your changes.
Running the ODataConsoleApplication with simple CRUD operations for asset major types
- Select ODataConsoleApplication project in Solution Explorer, Right click > Set as Startup Project.
- Open the Program.cs file under ODataConsoleApplication
- Comment all lines except SimpleCRUDExamples.SimpleCRUD(context).
- Add 3 breakpoints in SimpleCRUDExamples() method for d365.SaveChanges() lines 23, 34 and 43.
- Run the code and on first breakpoint have a look in Dynamics 365 for Finance and Operations Fixed assets > Setup > Fixed asset attributes > Major types and check Test01 is not created yet.
- Then back in Visual Studio, press F10 to step over and refresh your browser, you can now see new Test01 major type is created.
- Back in back in Visual Studio again, press F5 to go to the next breakpoint and then F10 to step over (almost there!!!).
- Check asset major types and description field is now updated from Description of Test01 to Updated description.
- Press F5 twice to finish the execution, asset major type Test01 is deleted.
- Review the console output.
In summary, I have explained how to configure and consume simple CRUD operations for the asset major type data entity using OData services and .NET application. Obviously we used the asset major type as an example but you can use any standard data entity or even your custom data entity as long as isPublic property is set to Yes.
I did not cover the OData query options or save changes options which are quite powerful. In addition, feel free to have a look at the other projects for JSON, SOAP and tests, I am sure they are worth a try.
I hope you will find this post helpful and I look forward to any feedback or question you may have.
Also it would be interesting to compare data entities features with the AX2012 AIF document services. For example, what are the equivalent of prepareForSave(), processingRecord()… methods? How do you implement external codes for items, customers… as it was previously configuration only in AX2012? How to trigger a packing slip posting after importing multiple lines?
- Consume the OData API from an external console application
- OData services endpoint
- Register a native application with AAD
- Microsoft Dynamics AX Integration: A Customer Perspective (if you have access to Microsoft Dynamics Learning Portal)