Authentication and OData service call

OData Service call will need an authorization token passed as a header on the service request. Using ADAL library, retrieve the authentication token from the Azure Active directory. Using generated proxy classes, you can instantiate the data entity objects, set properties, and call methods to interact with the OData endpoint. The following sample code shows how to build the OData endpoint context, attach the authentication header, create a new customer group, and then read and print customer groups on console:

using D365FO_ODataServiceClient.Microsoft.Dynamics.DataEntities;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.OData.Client;
using System;
using System.Linq;
namespace D365FO_ODataServiceClient
{
class Program
{
static void Main(string[] args)
{
//Part 1 - Set Variables (Replace the variables values with
your
//environment URI)
const string UriString =
"https://XYZINCDEVENV.cloudax.dynamics.com";
const string ADTenant = "https://login.windows.net/XYZINC.com";
const string ADClntAppId = "YourAPPID";
const string ADClntSecret = "AppSecret";
// Part 2. Create context
Uri oDataUri = new Uri(UriString + "/data", UriKind.Absolute);
Resources context = new Resources(oDataUri);
//Part 3. Set Authorization Header
context.SendingRequest2 += new
EventHandler<SendingRequest2EventArgs>
(delegate (object sender, SendingRequest2EventArgs e)
{
var authContext = new AuthenticationContext(ADTenant);
var cred = new ClientCredential(ADClntAppId, ADClntSecret);
var result = authContext.AcquireTokenAsync(UriString,
cred).Result;
e.RequestMessage.SetHeader("Authorization",
result.CreateAuthorizationHeader());
});
// Part 4: Create data in Operations - create Customer
group -create
//entity object
CustomerGroup customerGroup = new CustomerGroup();
//create collection and set context
DataServiceCollection<CustomerGroup> custGroupCollection
= new DataServiceCollection<CustomerGroup>(context);
// add to collection
custGroupCollection.Add(customerGroup);
// Set properties
customerGroup.CustomerGroupId = new
Random().Next(1000).ToString("000");
customerGroup.Description = "Brand new group";
customerGroup.DataAreaId = "USMF";
// save changes
context.SaveChanges(SaveChangesOptions.PostOnlySetProperties |
SaveChangesOptions.BatchWithSingleChangeset);
//Part 5. Reading data from Operations
DataServiceQuery<CustomerGroup> usmfCustomerGroup;
usmfCustomerGroup = context.CustomerGroups
.AddQueryOption("$filter", "dataAreaId eq 'USMF'")
.AddQueryOption("cross-company", "true");
foreach (var custGroup in usmfCustomerGroup)
{
Console.WriteLine("Name:{0} {1}",
custGroup.CustomerGroupId,
custGroup.DataAreaId);
}
Console.ReadLine();
}
}
}

The preceding code can be understood in five parts:

  • Part 1: Configure your Dynamics 365 for Finance and Operations base URI, active directory tenant, and authentication details
  • Part 2: OData Service context is created using OData endpoint URI
  • Part 3: Trigger an OData service request, acquire an authentication token using AAD tenant and credentials, and set the authorization header to the request
  • Part 4: Create a new customer group record, instantiate the CustomerGroup entity, create data collection and set context, add the entity to the collection, set the properties, and call the context.SaveChanges() method to save the new customer group changes to the Finance and Operations database
  • Part 5: Use LINQ expression to instantiate and query the CustomerGroup OData service, and print the ID and company as output
The mentioned steps and sample code are high-level steps to understand the integration concept and does not cover exception handling. To compile and execute this code, you must use your development environment and follow the steps as described in this section (registering application, creating console application, registering the assemblies, adding and generating template code, and finally, replacing the variables in the code).
The Microsoft product team has developed more sophisticated examples, which are available on GitHub. The sample code can be downloaded from https://github.com/Microsoft/Dynamics-AX-Integration.
..................Content has been hidden....................

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