/// <summary> /// Retrieves the BulkDeleteOperation, but it's not necessarily created /// immediately, so this method uses polling. /// </summary> /// <param name="bulkQuery">the query to find the BulkDeleteOperation.</param> /// <param name="entityCollection">the initial results of the query.</param> /// <param name="operationEndedStatus"> /// the statecode that will indicate that the operation has ended. /// </param> private BulkDeleteOperation RetrieveBulkDeleteOperation( QueryByAttribute bulkQuery, EntityCollection entityCollection, BulkDeleteOperationState operationEndedStatus) { BulkDeleteOperation createdBulkDeleteOperation = null; // Monitor the async operation via polling until it is complete or max // polling time expires. const int ARBITRARY_MAX_POLLING_TIME = 60; int secondsTicker = ARBITRARY_MAX_POLLING_TIME; while (secondsTicker > 0) { // Make sure the async operation was retrieved. if (entityCollection.Entities.Count > 0) { // Grab the one bulk operation that has been created. createdBulkDeleteOperation = (BulkDeleteOperation)entityCollection.Entities[0]; // Check the operation's state. // NOTE: If a recurrence for the BulkDeleteOperation was // specified, the state of the operation will be Suspended, // not Completed, since the operation will run again in the // future. if (createdBulkDeleteOperation.StateCode != operationEndedStatus) { // The operation has not yet completed. Wait a second for // the status to change. System.Threading.Thread.Sleep(1000); secondsTicker--; // Retrieve a fresh version of the bulk delete operation. entityCollection = _serviceProxy.RetrieveMultiple(bulkQuery); } else { // Stop polling as the operation's state is now complete. secondsTicker = 0; Console.WriteLine( " The BulkDeleteOperation record has been retrieved."); } } else { // Wait a second for async operation to activate. System.Threading.Thread.Sleep(1000); secondsTicker--; // Retrieve the entity again. entityCollection = _serviceProxy.RetrieveMultiple(bulkQuery); } } return(createdBulkDeleteOperation); }
/// <summary> /// Retrieves the BulkDeleteOperation, but it's not necessarily created /// immediately, so this method uses polling. /// </summary> /// <param name="bulkQuery">the query to find the BulkDeleteOperation.</param> /// <param name="entityCollection">the initial results of the query.</param> /// <param name="operationEndedStatus"> /// the statecode that will indicate that the operation has ended. /// </param> private BulkDeleteOperation RetrieveBulkDeleteOperation( QueryByAttribute bulkQuery, EntityCollection entityCollection, BulkDeleteOperationState operationEndedStatus) { BulkDeleteOperation createdBulkDeleteOperation = null; // Monitor the async operation via polling until it is complete or max // polling time expires. const int ARBITRARY_MAX_POLLING_TIME = 60; int secondsTicker = ARBITRARY_MAX_POLLING_TIME; while (secondsTicker > 0) { // Make sure the async operation was retrieved. if (entityCollection.Entities.Count > 0) { // Grab the one bulk operation that has been created. createdBulkDeleteOperation = (BulkDeleteOperation) entityCollection.Entities[0]; // Check the operation's state. // NOTE: If a recurrence for the BulkDeleteOperation was // specified, the state of the operation will be Suspended, // not Completed, since the operation will run again in the // future. if (createdBulkDeleteOperation.StateCode != operationEndedStatus) { // The operation has not yet completed. Wait a second for // the status to change. System.Threading.Thread.Sleep(1000); secondsTicker--; // Retrieve a fresh version of the bulk delete operation. entityCollection = _serviceProxy.RetrieveMultiple(bulkQuery); } else { // Stop polling as the operation's state is now complete. secondsTicker = 0; Console.WriteLine( " The BulkDeleteOperation record has been retrieved."); } } else { // Wait a second for async operation to activate. System.Threading.Thread.Sleep(1000); secondsTicker--; // Retrieve the entity again. entityCollection = _serviceProxy.RetrieveMultiple(bulkQuery); } } return createdBulkDeleteOperation; }
/// <summary> /// Inspect and display information about the created BulkDeleteOperation. /// </summary> /// <param name="createdBulkDeleteOperation"> /// the BulkDeleteOperation to inspect. /// </param> /// <param name="bulkOperationEnded"> /// the statecode that will tell us if the BulkDeleteOperation is ended. /// </param> /// <param name="bulkOperationSuccess"> /// the statuscode that will tell us if the BulkDeleteOperation was successful. /// </param> /// <param name="useRecurrence"> /// whether or not the BulkDeleteOperation is a recurring operation. /// </param> private void InspectBulkDeleteOperation( BulkDeleteOperation createdBulkDeleteOperation, BulkDeleteOperationState bulkOperationEnded, bulkdeleteoperation_statuscode bulkOperationSuccess, bool useRecurrence) { // Validate that the operation was completed. if (createdBulkDeleteOperation.StateCode != bulkOperationEnded) { // This will happen if it took longer than the polling time allowed // for this operation to complete. Console.WriteLine(" Completion of the Bulk Delete took longer\n" + " than the polling time allotted."); } else if (createdBulkDeleteOperation.StatusCode.Value != (int)bulkOperationSuccess) { Console.WriteLine(" The Bulk Delete operation failed."); } else if (!useRecurrence) { // Check for the number of successful deletes. var successfulDeletes = createdBulkDeleteOperation.SuccessCount ?? 0; Console.WriteLine(" {0} records were successfully deleted", successfulDeletes); // Check for any failures that may have occurred during the bulk // delete operation. if (createdBulkDeleteOperation.FailureCount > 0) { Console.WriteLine(" {0} records failed to be deleted:", createdBulkDeleteOperation.FailureCount); // Query for all the failures. var failureQuery = new QueryByAttribute(); failureQuery.ColumnSet = new ColumnSet(true); failureQuery.EntityName = BulkDeleteFailure.EntityLogicalName; failureQuery.Attributes.Add("bulkdeleteoperationid"); var bulkDeleteOperationId = createdBulkDeleteOperation.BulkDeleteOperationId ?? Guid.Empty; failureQuery.Values.Add(bulkDeleteOperationId); // Retrieve the bulkdeletefailure objects. EntityCollection entityCollection = _serviceProxy.RetrieveMultiple( failureQuery); // Examine each failure for information regarding the failure. foreach (BulkDeleteFailure failureOperation in entityCollection.Entities) { // Process failure information. Console.WriteLine(String.Format( " {0}, {1}", failureOperation.RegardingObjectId.Name, failureOperation.RegardingObjectId.Id)); } } } else { // NOTE: If recurrence is used, we cannot reliably retrieve data // about the records that were deleted, since a sub-BulkDeleteOperation // is created by Microsoft Dynamics CRM that does not have any fields tying it back to the // Asynchronous operation or the BulkDeleteOperation. This makes it // unreliable to know which subprocess to retrieve. Console.WriteLine(" The recurring Bulk Delete Operation was created successfully."); } }