1. Create a function which accept list of entity
public static void BulkUpload(List<Entity> _ListEntity)
{
ExecuteMultipleRequest 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 = new EntityCollection(_ListEntity);
// 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);
}
CrmServiceConnector crmServiceConnector = new CrmServiceConnector();
// crmServiceConnector.CallMultitpleExecuteMethod(requestWithResults, null, false);
// Execute all the requests in the request collection using a single web method call.
ExecuteMultipleResponse responseWithResults = crmServiceConnector.CallMultitpleExecuteMethod(requestWithResults, null, false);
// 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);
}
}
2. Function to execute request
public ExecuteMultipleResponse CallMultitpleExecuteMethod(ExecuteMultipleRequest request, string organizationName, bool useAdminCredentials)
{
if (!isConnected)
{
Connect(useAdminCredentials);
}
try
{
ExecuteMultipleResponse responseWithResults =
(ExecuteMultipleResponse)service.Execute(request);
return responseWithResults;
}
catch (Exception exception)
{
StringBuilder parameterInfo = new StringBuilder();
parameterInfo.Append(String.Format("request : {0}", (request == null) ? "request Null" : request.ToString()));
return null;
// throw ExceptionMethods.ProcessWebServiceException(exception, WebServiceCallParameterInfo, parameterInfo.ToString());
}
}
public static void BulkUpload(List<Entity> _ListEntity)
{
ExecuteMultipleRequest 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 = new EntityCollection(_ListEntity);
// 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);
}
CrmServiceConnector crmServiceConnector = new CrmServiceConnector();
// crmServiceConnector.CallMultitpleExecuteMethod(requestWithResults, null, false);
// Execute all the requests in the request collection using a single web method call.
ExecuteMultipleResponse responseWithResults = crmServiceConnector.CallMultitpleExecuteMethod(requestWithResults, null, false);
// 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);
}
}
2. Function to execute request
public ExecuteMultipleResponse CallMultitpleExecuteMethod(ExecuteMultipleRequest request, string organizationName, bool useAdminCredentials)
{
if (!isConnected)
{
Connect(useAdminCredentials);
}
try
{
ExecuteMultipleResponse responseWithResults =
(ExecuteMultipleResponse)service.Execute(request);
return responseWithResults;
}
catch (Exception exception)
{
StringBuilder parameterInfo = new StringBuilder();
parameterInfo.Append(String.Format("request : {0}", (request == null) ? "request Null" : request.ToString()));
return null;
// throw ExceptionMethods.ProcessWebServiceException(exception, WebServiceCallParameterInfo, parameterInfo.ToString());
}
}
No comments:
Post a Comment