3
votes

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.

3
On which version are you working? Do you need to create records in a single transaction?Henk van Boeijen
@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 Patil

3 Answers

3
votes

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
votes

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
votes

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.