3

I know how to create a single entity in single request. However, one requirement wants me to create multiple entities (in my case it's multiple entries in ContactSet). I tried putting array to

POST /XRMServices/2011/OrganizationData.svc/ContactSet

[{
    "MobilePhone": "+0012 555 555 555",
    "YomiFullName" : "Demo User 1",
    "GenderCode" : {
        "Value" : 1
      }
     .....
     <data removed for sanity>
     .....    
},
 {
    "MobilePhone": "+0012 555 555 111",
    "YomiFullName" : "Demo User 2",
    "GenderCode" : {
        "Value" : 1
      }
     .....
     <data removed for sanity>
     .....    

}]

However this does not work and I could not find any documentation explaining me ways to achieve this. Any help would be greatly appreciated.

2
  • On which version are you working? Do you need to create records in a single transaction? Commented Sep 11, 2016 at 18:56
  • @HenkvanBoeijen I am working on Dynamics CRM 2016. Unfortunately I am limited to either WebApi or Organization Data Service (XRMServices/2011/OrganizationData.svc) so want to achieve it through those options only. I want to know if there is a way to create multiple records in a single transaction? I am sort of importing contact from other system to CRM so need to write importer kinda utility.
    – Rahul
    Commented Sep 12, 2016 at 6:02

3 Answers 3

3

You need to use an ExecuteMultipleRequest, I don't believe this is available in Rest service however, but is available in the SOAP service.

// Get a reference to the organization service.
using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials))
{
    // Enable early-bound type support to add/update entity records required for this sample.
    _serviceProxy.EnableProxyTypes();

    #region Execute Multiple with Results
    // Create an ExecuteMultipleRequest object.
    requestWithResults = 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()
    };

    // Create several (local, in memory) entities in a collection. 
    EntityCollection input = GetCollectionOfEntitiesToCreate();

    // Add a CreateRequest for each entity to the request collection.
    foreach (var entity in input.Entities)
    {
        CreateRequest createRequest = new CreateRequest { Target = entity };
        requestWithResults.Requests.Add(createRequest);
    }

    // Execute all the requests in the request collection using a single web method call.
    ExecuteMultipleResponse responseWithResults =
        (ExecuteMultipleResponse)_serviceProxy.Execute(requestWithResults);

    // Display the results returned in the responses.
    foreach (var responseItem in responseWithResults.Responses)
    {
        // A valid response.
        if (responseItem.Response != null)
            DisplayResponse(requestWithResults.Requests[responseItem.RequestIndex], responseItem.Response);

        // An error has occurred.
        else if (responseItem.Fault != null)
            DisplayFault(requestWithResults.Requests[responseItem.RequestIndex], 
                responseItem.RequestIndex, responseItem.Fault);
    }
}
3
  • Thanks for the answer James. Isn't this something similar to batch operations?
    – Rahul
    Commented Sep 11, 2016 at 11:02
  • 1
    @RahulPatil, I'm not entirely sure what you mean, but I think the answer is yes.
    – James Wood
    Commented Sep 11, 2016 at 17:22
  • It is good to add that the ExecuteMultipleRequest is of limited use. The requests it contains do not participate in a single transaction and are executed sequentially, so the only gain here is less round-trips to the server. Commented Sep 11, 2016 at 19:05
3

ExecuteMultipleRequest is a good but not the only way. If you use CRM 2016 you can use Batch operations that is available in new WebApi. Check article that describes it - https://msdn.microsoft.com/en-us/library/mt607719.aspx

1

You can use a Web API action (see MSDN) to execute an ExecuteTransactionRequest, as described here. Subject of the example on MSDN is the WinOpportunityRequest, but it should work with any supported request, including custom actions.

Not the answer you're looking for? Browse other questions tagged or ask your own question.