How to do it...

  1. Using the plugin registration tool, register the following steps on your Azure Service Bus endpoint:
    • Message: Retrieve
    • Primary Entity: account
    • Execution Mode: Asynchronous
    • Click on Register New Step
  1. In Visual Studio, create a new console application called Packt.Xrm.ReadAudit.
  2. Add the following four NuGet packages:
    • Microsoft.Azure.DocumentDB
    • Windows.Azure.ServiceBus
    • Newtonsoft.JSON
    • Microsoft.CrmSdk.CoreAssemblies
  1. Add the following using statements:
using Microsoft.Azure.Documents.Client; 
using Microsoft.ServiceBus.Messaging;
using Newtonsoft.Json.Linq;
using Microsoft.Xrm.Sdk;
  1. Add the following private constant variables:
private const string endpointUri = "https://.documents.azure.com:443/"; 
private const string primaryKey = "<Your Primary Key>";
private const string databaseName = "Dynamics365";
private const string collectionName = "Audit";
private const string connectionString = "Endpoint=sb://.servicebus.windows.net/;
SharedAccessKeyName=CrmQueue;SharedAccessKey=;EntityPath= ";
private const string queueName = "dynamics365queue";
private static DocumentClient docClient;
  1. Add the following code in your main method:
var client = QueueClient.CreateFromConnectionString(connectionString, queueName); 
docClient = new DocumentClient(new Uri(endpointUri), primaryKey);
var dbUri = UriFactory.CreateDocumentCollectionUri(databaseName, collectionName);

client.OnMessage(message =>
{
var result = message.GetBody<RemoteExecutionContext>();
var readLog = $"{{userId:'{
result.InitiatingUserId}',entity:'{
result.PrimaryEntityName}',entityId:'{
result.PrimaryEntityId}'}}";
var parsedReadLog = JObject.Parse(readLog);
docClient.CreateDocumentAsync(dbUri, parsedReadLog).Wait();
}
);
Console.WriteLine("Press [Enter] to terminate");
Console.ReadLine();
client.Close();
  1. Run your console application.
  2. Retrieve an account record in Dynamics 365.
  3. Query your Cosmos DB in Azure for new entries.
..................Content has been hidden....................

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