【问题标题】:Creating multiple entities in single request in Microsoft Dynamics CRM (OData)在 Microsoft Dynamics CRM (OData) 中的单个请求中创建多个实体
【发布时间】:2016-09-11 07:40:45
【问题描述】:

我知道如何在单个请求中创建单个实体。但是,一项要求是我要创建多个实体(在我的情况下,它是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>
     .....    

}]

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

【问题讨论】:

  • 您在哪个版本上工作?您需要在单个事务中创建记录吗?
  • @HenkvanBoeijen 我正在开发 Dynamics CRM 2016。不幸的是,我仅限于 WebApi 或组织数据服务 (XRMServices/2011/OrganizationData.svc),因此我只想通过这些选项来实现它。我想知道是否有办法在单个事务中创建多个记录?我正在将联系人从其他系统导入 CRM,因此需要编写导入器实用程序。

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


【解决方案1】:

您需要使用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);
    }
}

【讨论】:

  • 感谢詹姆斯的回答。这不是类似于批处理操作吗?
  • @RahulPatil,我不完全确定你的意思,但我认为答案是肯定的。
  • 补充一下ExecuteMultipleRequest 的用途有限。它包含的请求不参与单个事务并按顺序执行,因此这里唯一的好处是减少了与服务器的往返次数。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 2015-12-22
      • 2021-07-17
      • 1970-01-01
      相关资源
      最近更新 更多