Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating multiple entities in single request in Microsoft Dynamics CRM (OData)

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.

like image 655
Rahul Patil Avatar asked Oct 25 '25 18:10

Rahul Patil


1 Answers

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);
    }
}
like image 169
James Wood Avatar answered Oct 29 '25 20:10

James Wood