CRM 2013 – Execute Asynchronous Operations

Roshan Mehta, 24 October 2013

The new Dynamics CRM 2013 SDK includes a new method to execute service requests asynchronously. Please note that this is only currently supported for the ImportSolutionRequest but we can expect this to be extended in the near future to support many other types of requests in CRM. 

The benefit of executing requests asynchronously is that system performance can greatly increase since the request can be executed when there is more server resource available. The response of an asynchronous operation includes an AsyncJobId property which you can use to query the database in order to find out what state the job is currently in.

The following code snippet shows how we can execute an asynchronous create request in CRM. Again I stress that this is currently unsupported at the time of this writing and is merely an example of what the code may look like.

var connection = new CrmConnection("Crm");
var service = new OrganizationService(connection);
var context = new CrmOrganizationServiceContext(connection);

IOrganizationService sdk = (IOrganizationService)context;

Entity contact = new Entity("contact");
contact["firstname"] = "Roshan";
contact["lastname"] = "Mehta";

Console.WriteLine("Creating Contact asynchronously");

ExecuteAsyncRequest request = new ExecuteAsyncRequest
{
    Request = new CreateRequest
    {
        Target = contact
    }
};

ExecuteAsyncResponse response = (ExecuteAsyncResponse)sdk.Execute(request);
Console.WriteLine("Contact created");

When you run this code snippet, the platform will throw an error indicating that the CreateRequest is unsupported as an asynchronous request.