在Microsoft Dynamics CRM(OData)中的单个请求中创建多个实体
我知道如何在单个请求中创建单个实体。然而,有一个需求需要我创建多个实体(在我的例子中,它是在Microsoft Dynamics CRM(OData)中的单个请求中创建多个实体,odata,dynamics-crm,dynamics-crm-2016,Odata,Dynamics Crm,Dynamics Crm 2016,我知道如何在单个请求中创建单个实体。然而,有一个需求需要我创建多个实体(在我的例子中,它是联系人集中的多个条目)。我试着把数组放到 POST/XRMServices/2011/OrganizationData.svc/ContactSet [{ "MobilePhone": "+0012 555 555 555", "YomiFullName" : "Demo User 1", "GenderCode" : { "Value" : 1 }
联系人集
中的多个条目)。我试着把数组放到
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>
.....
}]
[{
“手机”:“+001255555555”,
“YomiFullName”:“演示用户1”,
“性别代码”:{
“价值”:1
}
.....
.....
},
{
“手机”:“+0012555555111”,
“YomiFullName”:“演示用户2”,
“性别代码”:{
“价值”:1
}
.....
.....
}]
然而,这不起作用,我找不到任何文档来解释如何实现这一点。任何帮助都将不胜感激。您需要使用,但我不相信这在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);
}
}
ExecuteMultipleRequest是一种好方法,但不是唯一的方法。如果您使用CRM 2016,则可以使用新WebApi中提供的批处理操作。查看描述它的文章-您可以使用Web API操作()执行
ExecuteTransactionRequest
,如前所述。MSDN上示例的主题是WinOpportunityRequest
,但它应该适用于任何受支持的请求,包括自定义操作。感谢您的回答。这不是类似于批处理操作吗?@RahulPatil,我不完全确定你的意思,但我认为答案是肯定的。最好补充一点,ExecuteMultipleRequest
的用途有限。它包含的请求不参与单个事务,而是按顺序执行,因此这里唯一的好处是减少到服务器的往返次数。您使用的是哪个版本?您需要在单个事务中创建记录吗?@HenkvanBoeijen我正在处理Dynamics CRM 2016。不幸的是,我仅限于WebApi或组织数据服务(XRMServices/2011/OrganizationData.svc
),所以我只想通过这些选项来实现它。我想知道是否有一种方法可以在一个事务中创建多个记录?我正在从其他系统导入联系人到CRM,所以需要编写类似于导入器的实用程序。