在Microsoft Dynamics CRM(OData)

时间:2016-09-11 07:40:46

标签: odata dynamics-crm dynamics-crm-2016

我知道如何在单个请求中创建单个实体。但是,有一个要求要我创建多个实体(在我的例子中,它是ContactSet中的多个条目)。我尝试将数组放到

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>
     .....    

}]

然而,这不起作用,我找不到任何文件解释我实现这一目标的方法。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:3)

您需要使用ExecuteMultipleRequest,但我不认为这在Rest服务中可用,但在SOAP服务中可用。

// 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);
    }
}

答案 1 :(得分:3)

ExecuteMultipleRequest是一个很好但不是唯一的方法。如果您使用CRM 2016,则可以使用新WebApi中提供的批处理操作。查看描述它的文章 - https://msdn.microsoft.com/en-us/library/mt607719.aspx

答案 2 :(得分:1)

您可以使用Web API操作(see MSDN)执行ExecuteTransactionRequest,如here所述。 MSDN上示例的主题是WinOpportunityRequest,但它应该适用于任何支持的请求,包括自定义操作。