Contact

Industry Leading Blog for Manufacturers

If you’ve worked with the Microsoft Dynamics CRM import wizard you’ll know that although it is a very powerful tool, it does have its limitations.  The maximum file size for individual files is 8 MB, which can be a limiting factor when you need to import a large dataset. Dynamics CRM attempts to mitigate this issue by allowing users to upload a zip file with a maximum size of 32 MB, but still requires you to split your files into 8 MB chunks inside of that zip file.

In addition to file size limitation the import wizard is also limited in the operation it can perform.  Using the import wizard you can only create new records unless you have already done an export from CRM and chose the option to export for re-import.  We also find limitations when our imports require complicated assignment requests, do multiple look ups, or needs to use complex rules to assign values.

Oftentimes the solution to these limitations is to write a custom data import tool. In the past, these data import tools were forced to create or update each entity one at a time, issuing one request per operation which could cause excruciatingly long import times.

With Dynamics 2013, the Dynamics CRM API now supports issuing requests in bulk using the ExecuteMultipleRequest. This request will allow you to batch up to 1,000 requests to be issued all at once, vastly improving the performance of custom data import tools.

The following ExecuteMultipleRequests C# method demonstrates how to use the ExecuteMultipleRequest and handle successes and errors for the requests being made:

// using System.Collections.Generic;
// using Microsoft.Xrm.Client;
// using Microsoft.Xrm.Sdk;
// using Microsoft.Xrm.Sdk.Messages;
///
/// Uses an ExecuteMultipleRequest to issue many OrganizationRequests
/// in a single request to CRM.
///
///
/// An existing connection to Dynamics CRM.
///
///
/// A collection of requests to execute using an ExecuteMultipleRequest.
/// This collection can have a maximum of 1000 requests in it.
///
public void ExecuteMultipleRequests(CrmOrganizationServiceContext connection,
IEnumerable< OrganizationRequest > requests)
{
var executeMultiple = new ExecuteMultipleRequest();
// All of the create or update requests are added to the
// "Request" property of the ExecuteMultipleRequest.
executeMultiple.Requests = new OrganizationRequestCollection();
// It is important to note that each ExecuteMultipleRequest can
// only include a maximum of 1000 requests. To handle more requests,
// batch your requests into groups of 1000 and create an
// ExecuteMultipleRequest for each.
executeMultiple.Requests.AddRange(requests);
executeMultiple.Settings = new ExecuteMultipleSettings
{
ContinueOnError = true, // When false, stop issuing requests on the first fault.
ReturnResponses = true // When false, responses (see below) are not returned.
};
// Execute the request to get the results
var executionResponse = connection.Execute(executeMultiple);
// Get the actual responses from the results
var responses =
(ExecuteMultipleResponseItemCollection)executionResponse.Results["Responses"
];
foreach (var response in responses)
{
if (response.Fault != null)
{
// A fault has occurred, handle it here
}
else
{
// No fault, executed successfully
}
}
}

This method takes a list of OrganizationRequest (CreateRequest, UpdateRequest, or AssignRequest, for example) objects and issues them using the ExecuteMultipleRequest. These OrganizationRequests can easily be created using the Dynamics CRM API:

// using Microsoft.Xrm.Sdk.Messages;
// using Microsoft.Xrm.Client;
// Sample, create an entity creation request for the given entity.
var entityCreateRequest = new CreateRequest()
{
Target = entityToCreate
};
// Sample, create an entity update request for the given entity with
// changed values.
var entityUpdateRequest = new UpdateRequest()
{
Target = entityToUpdate
};

The following sample code shows how to use this ExecuteMutlipleRequests method in the context of a custom data import program: 

// using Microsoft.Xrm.Client;
///
/// A sample method that mimics the steps of a data import.
///
 
///
/// An existing connection to Dynamics CRM.
///
public static void ExecuteImport(CrmOrganizationServiceContext connection)
{
// Read in the file with the import data (.csv, .xml, etc)
// TODO: LoadImportFile method left to the reader to write
var importData = LoadImportFile();
// Transform the import file into Dynamics CRM Entity instances
// TODO: GetEntitiesToBatch left to the reader to write
var entitiesToBatch = GetEntitiesToBatch(importData);
// Convert the list of Entity objects to OrganizationRequest objects
// TODO: GetRequestsToBatch left to the reader to write
var requestsToBatch = GetRequestsToBatch(entitiesToBatch);
// Begin the import
ExecuteMultipleRequests(connection, requestsToBatch);
}

While this method will drastically improve import times for custom data import tools, it is important to note the limitations of this type of request:

  • An ExecuteMultipleRequest cannot be nested within another ExecuteMultipleRequest.
  • No more than 1,000 requests can be added to a single ExecuteMultipleRequest, unless this value is overridden.
  • No more than 2 ExecuteMultipleRequests can be executing at the same time, unless this value is overridden.


Stay up to date on the leading industry solutions by subscribing to our blog digest.
What can we help you with?

CONTACT US

(585) 506-4600