public override void ProcessData() { var successfulRequests = 0; var attributeData = InputFile.Worksheets[ConfigurationFile.WorkSheets.Attributes].Data; var oneToManyData = InputFile.Worksheets[ConfigurationFile.WorkSheets.OneToManyRelationships].Data; var manyToManyData = InputFile.Worksheets[ConfigurationFile.WorkSheets.ManyToManyRelationships].Data; if (attributeData.Count > 0 || oneToManyData.Count > 0 || manyToManyData.Count > 0) { var attributeRelationshipRequests = new OrganizationRequestCollection(); attributeRelationshipRequests.AddRange(MapDataToRequests(MapAttributes, attributeData)); attributeRelationshipRequests.AddRange(MapDataToRequests(MapOneToMany, oneToManyData)); attributeRelationshipRequests.AddRange(MapDataToRequests(MapManyToMany, manyToManyData)); successfulRequests = ExecuteRequests(attributeRelationshipRequests); } var optionSetData = InputFile.Worksheets[ConfigurationFile.WorkSheets.OptionSets].Data; var entityData = InputFile.Worksheets[ConfigurationFile.WorkSheets.Entities].Data; if (entityData.Count > 0 || optionSetData.Count > 0) { var entitiesRequests = new OrganizationRequestCollection(); entitiesRequests.AddRange(MapDataToRequests(MapEntities, entityData)); entitiesRequests.AddRange(MapDataToRequests(MapOptionSets, optionSetData)); successfulRequests = ExecuteRequests(entitiesRequests); } if (successfulRequests > 0) { Service.Execute(new PublishAllXmlRequest()); } }
public override void ProcessData() { var successfulRequests = 0; var optionSetData = InputFile.Worksheets[ConfigurationFile.WorkSheets.OptionSets].Data; var entityData = InputFile.Worksheets[ConfigurationFile.WorkSheets.Entities].Data; if (entityData.Count > 0 || optionSetData.Count > 0) { var entityRequests = new OrganizationRequestCollection(); var entityMapper = new CreateEntityRequestDataMapper(InputFile.Worksheets[ConfigurationFile.WorkSheets.Entities].Columns, OrgLcId); entityRequests.AddRange(MapDataToRequests(entityMapper, entityData)); var optionSetMapper = new CreateOptionSetRequestDataMapper(InputFile.Worksheets[ConfigurationFile.WorkSheets.OptionSets].Columns, OrgLcId); entityRequests.AddRange(MapDataToRequests(optionSetMapper, optionSetData)); successfulRequests = ExecuteRequests(entityRequests); } var attributeData = InputFile.Worksheets[ConfigurationFile.WorkSheets.Attributes].Data; var oneToManyData = InputFile.Worksheets[ConfigurationFile.WorkSheets.OneToManyRelationships].Data; var manyToManyData = InputFile.Worksheets[ConfigurationFile.WorkSheets.ManyToManyRelationships].Data; if (attributeData.Count > 0 || oneToManyData.Count > 0 || manyToManyData.Count > 0) { var otherRequests = new OrganizationRequestCollection(); var attributeMapper = new CreateAttributeRequestDataMapper(InputFile.Worksheets[ConfigurationFile.WorkSheets.Attributes].Columns, OrgLcId); otherRequests.AddRange(MapDataToRequests(attributeMapper, attributeData)); var oneToManyMapper = new CreateOneToManyRequestDataMapper(InputFile.Worksheets[ConfigurationFile.WorkSheets.OneToManyRelationships].Columns, OrgLcId); otherRequests.AddRange(MapDataToRequests(oneToManyMapper, oneToManyData)); var manyToManyMapper = new CreateManyToManyRequestDataMapper(InputFile.Worksheets[ConfigurationFile.WorkSheets.ManyToManyRelationships].Columns, OrgLcId); otherRequests.AddRange(MapDataToRequests(manyToManyMapper, manyToManyData)); successfulRequests += ExecuteRequests(otherRequests); } if (successfulRequests > 0) { Service.Execute(new PublishAllXmlRequest()); } }
public BulkResponse Write(IOrganizationService service, Dictionary <string, OrganizationRequest> requestsToBeProcessed) { BulkResponse bulkWriteResponse = new BulkResponse(); bulkWriteResponse.Responses = new List <BulkResponseItem>(); int batchNumber = 0; int recordsToProcessCount = requestsToBeProcessed.Count; while (recordsToProcessCount > _batchSize || recordsToProcessCount > 0) { Dictionary <string, OrganizationRequest> currentBatchOfRequests = requestsToBeProcessed.Skip(batchNumber * _batchSize).Take(_batchSize).ToDictionary(x => x.Key, x => x.Value); OrganizationRequestCollection orgReqs = new OrganizationRequestCollection(); orgReqs.AddRange(currentBatchOfRequests.Values.ToList()); ExecuteMultipleResponse response = ExecuteMultiple(service, orgReqs); List <BulkResponseItem> bulkResponses = ReadExecuteMultipleResponse(response, currentBatchOfRequests); bulkWriteResponse.Responses.AddRange(bulkResponses); bulkWriteResponse.HasErrors = response.IsFaulted; recordsToProcessCount = recordsToProcessCount - currentBatchOfRequests.Count; batchNumber++; } return(bulkWriteResponse); }
protected override void ExecuteMultipleWithRetry(List <EntityWrapper> entities, Func <EntityWrapper, OrganizationRequest> orgRequest) { var requests = new OrganizationRequestCollection(); requests.AddRange(entities.Select(wrapper => orgRequest(wrapper)).ToArray()); int cnt = 0; List <ExecuteMultipleResponseItem> responseWithResults = new List <ExecuteMultipleResponseItem>(); foreach (OrganizationRequest request in requests) { try { OrganizationResponse response = new UpsertResponse() { }; responseWithResults.Add(new ExecuteMultipleResponseItem() { Response = response, RequestIndex = cnt }); } catch (FaultException <OrganizationServiceFault> ex) { responseWithResults.Add(new ExecuteMultipleResponseItem() { Fault = ex.Detail, RequestIndex = cnt }); } cnt++; } PopulateExecutionResults(entities, responseWithResults); }
private void ExecuteMultipleRequests(List <OrganizationRequest> requests) { WorkAsync(new WorkAsyncInfo { Message = "Creating entity records", Work = (worker, args) => { var responses = new ExecuteMultipleResponseItemCollection(); var lists = SplitList(requests); var count = 0; foreach (var list in lists) { var collection = new OrganizationRequestCollection(); collection.AddRange(list); var req = new ExecuteMultipleRequest { Requests = collection, Settings = new ExecuteMultipleSettings { ContinueOnError = true, ReturnResponses = false } }; count++; var response = (ExecuteMultipleResponse)Service.Execute(req); SendMessageToStatusBar?.Invoke(this, new StatusBarMessageEventArgs(count / requests.Count, $"{count}/{requests.Count} records created")); responses.AddRange(response.Responses); } args.Result = responses; }, PostWorkCallBack = (args) => { if (args.Error != null) { MessageBox.Show($"Something bad happened. Error message:\n{args.Error}", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var result = (ExecuteMultipleResponseItemCollection)args.Result; if (result.Count == 0) { MessageBox.Show($"{requests.Count} records successfully created"); } else { var createdCount = requests.Count - result.Count; MessageBox.Show($"{createdCount}/{requests.Count} records created. See error log for details."); foreach (var response in result) { LogError(response.Fault.Message); } } }, IsCancelable = true, }); }
/// <summary> /// Converts a collection of type <see cref="OrganizationRequest"/> to a single <see cref="ExecuteMultipleRequest"/> instance /// </summary> /// <typeparam name="T">The typeof<see cref="OrganizationRequest"/></typeparam> /// <param name="requests">The collection of requests representing the batch</param> /// <param name="batchSettings">The desired settings</param> /// <returns>A single <see cref="ExecuteMultipleRequest"/> instance</returns> public static ExecuteMultipleRequest AsBatch <T>(this IEnumerable <T> requests, ExecuteMultipleSettings batchSettings) where T : OrganizationRequest { var batch = new OrganizationRequestCollection(); batch.AddRange(requests); return(new ExecuteMultipleRequest() { Requests = batch, Settings = batchSettings }); }
/// <summary> /// Executes an ExecuteMultipleRequest for a collection of requests with specified settings. /// </summary> /// <param name="service">IOrganizationService.</param> /// <param name="requestCollection">Collection of requests to execute.</param> /// <param name="requestSettings">The request settings.</param> /// <returns>ExecuteMultipleResponse.</returns> public static ExecuteMultipleResponse ExecuteMultiple(this IOrganizationService service, IEnumerable <OrganizationRequest> requestCollection, ExecuteMultipleSettings requestSettings) { if (requestCollection.Count() > 1000) { throw new ArgumentException("requestCollection must be 1000 requests or less"); } var executeCollection = new OrganizationRequestCollection(); executeCollection.AddRange(requestCollection); var request = new ExecuteMultipleRequest { Settings = requestSettings, Requests = executeCollection }; return((ExecuteMultipleResponse)service.Execute(request)); }
public BulkResponse Read(IOrganizationService service, Dictionary <string, QueryExpression> queriesToBeProcessed) { BulkResponse bulkWriteResponse = new BulkResponse(); bulkWriteResponse.Responses = new List <BulkResponseItem>(); Dictionary <string, OrganizationRequest> orgRequests = GetQueryOrgRequests(queriesToBeProcessed); OrganizationRequestCollection orgReqs = new OrganizationRequestCollection(); orgReqs.AddRange(orgRequests.Values.ToList()); ExecuteMultipleResponse response = ExecuteMultiple(service, orgReqs); List <BulkResponseItem> bulkResponses = ReadExecuteMultipleResponse(response, orgRequests); bulkWriteResponse.Responses.AddRange(bulkResponses); bulkWriteResponse.HasErrors = response.IsFaulted; return(bulkWriteResponse); }
public static void ExecuteMultiple(this IOrganizationService service, IEnumerable <OrganizationRequest> requests, int batchsize = 1000) { while (requests.Any()) { var batch = requests.Take(batchsize); requests = requests.Skip(batchsize); var requestCollection = new OrganizationRequestCollection(); requestCollection.AddRange(batch); var executemultiple = new ExecuteMultipleRequest { Requests = requestCollection, Settings = new ExecuteMultipleSettings() { ContinueOnError = false, ReturnResponses = false } }; service.Execute(executemultiple); //run the workflows } }
/// <summary> /// Create multiple test records. /// </summary> /// <param name="testData">The test records to create.</param> /// <returns>A collection of references to the created records.</returns> protected EntityReference[] CreateTestData(params Entity[] testData) { var requestCollection = new OrganizationRequestCollection(); var createRequests = testData.Select(t => new CreateRequest { Target = t }).ToArray(); requestCollection.AddRange(createRequests); var response = (ExecuteMultipleResponse)this.CrmServiceClient.Execute(new ExecuteMultipleRequest { Settings = new ExecuteMultipleSettings { ContinueOnError = true, ReturnResponses = true, }, Requests = requestCollection, }); var entityReferences = new List <EntityReference>(); for (int i = 0; i < response.Responses.Count; i++) { if (response.Responses[i].Fault != null) { throw new Exception(response.Responses[i].Fault.Message); } var entityReference = new EntityReference( createRequests[i].Target.LogicalName, ((CreateResponse)response.Responses.ElementAt(i).Response).id); this.CreatedEntities.Add(entityReference); entityReferences.Add(entityReference); } return(entityReferences.ToArray()); }
private void DeleteTestData() { var deleteRequests = new OrganizationRequestCollection(); if (this.CreatedEntities.Count == 0) { return; } deleteRequests.AddRange( this.CreatedEntities.Select( e => new DeleteRequest { Target = e })); this.CrmServiceClient.Execute(new ExecuteMultipleRequest { Settings = new ExecuteMultipleSettings { ContinueOnError = true, }, Requests = deleteRequests, }); }