Creating a project, adding libraries, and generating an OData service proxy

Once you are done with the application registration process and have collected all the details, you are ready to create your client application. In this example, we will create a console C# application to interact with Dynamics 365 for Finance and Operations OData services. The given steps describe how to create the client application:

  1. Create the C# console application: To start, use Visual Studio to create a C# console project. In Visual Studio, click on FileNew | Project.... In the new project dialog, use the Visual C# template, select Console Application, and enter the name as  D365FO_ODataServiceClient.
  1. Add reference libraries: To authenticate and consume Finance and Operations OData services, you need at least the following two library packages. You can use the NuGet package manager in Visual Studio to install these libraries in your project and add references:
    • Microsoft.IdentityModel.Clients.ActiveDirectory: This package contains the binaries of the Active Directory Authentication Library (ADAL). ADAL provides a .NET standard class library with easy-to-use authentication functionality for your .NET client.
    • Microsoft.OData.Client: This library provides a LINQ-enabled client API for issuing OData queries and consuming OData JSON payloads.
  2. Add OData v4 client code generator: This is the Visual Studio extension from Microsoft to generate OData entity proxy classes for your OData entities. If you do not have this extension already installed, you can download and install this template from Visual Studio Marketplace. After you download and install the tool, you can add the OData client to the project. This will create a file with an extension of tt (text template). Then, you need to update the MetadataDocumentUri string value in the tt file to your OData metadata endpoint. The next diagram illustrates the steps to add the OData Client tt file and update the MetadataDocumentURI in Visual Studio:

Once the metadata endpoint URL is updated, right-click on the tt file and choose to Run custom tool. This will read the metadata and build proxy classes for all OData services. This step generates a library containing all your OData services and their operations with the name like <YourProjectNameSpace>.Microsoft.Dynamics.DataEntities

Once the required packages are added to your project and the entity proxy is generated, next step is to implement the code to authenticate and consume OData entities. Let's explore the code sample next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset