Bulk / Batch Requests to CRM
public static ExecuteMultipleResponse BulkRequest(this IOrganizationService service, List<Entity> entities, Func<OrganizationRequest> newRequest)
{
// Create an ExecuteMultipleRequest object.
var multipleRequest = new ExecuteMultipleRequest()
{
// Assign settings that define execution behavior: continue on error, return responses.
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = true
},
// Create an empty organization request collection.
Requests = new OrganizationRequestCollection()
};
// Add a CreateRequest for each entity to the request collection.
foreach (var entity in entities)
{
OrganizationRequest request = newRequest();
request.Parameters["Target"] = entity;
multipleRequest.Requests.Add(request);
}
// Execute all the requests in the request collection using a single web method call.
return (ExecuteMultipleResponse)service.Execute(multipleRequest);
}