Wednesday, September 4, 2013

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using GCEntity = BT.CRM2011.DNS.Entities;
using System.ServiceModel.Description;

if (asyncOperation.StateCode != GCEntity.AsyncOperationState.Completed)
{
// We have to update the AsyncOperation to be in a Completed state
// before we can delete it.
asyncOperation.StateCode = GCEntity.AsyncOperationState.Completed;
CRMServiceProxy.Update(asyncOperation);
}

Console.WriteLine(" The AsyncOperation and BulkDeleteOperation have been deleted.");
}

private void InspectBulkDeleteOperation(
GCEntity.BulkDeleteOperation createdBulkDeleteOperation,
GCEntity.BulkDeleteOperationState bulkOperationEnded,
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 than the polling time allotted.");
}
else if (createdBulkDeleteOperation.StatusCode.Value!= 30)
{
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);

// Examine each failure for information regarding the failure.
foreach (GCEntity.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.");
}
}

// 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 = CRMServiceProxy.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--;