crm2011创建批量删除作业
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
/// <summary>
/// 创建批量删除作业
/// </summary>
public class CreateBulkDeleteRequest
{
public void Create(IOrganizationService service)
{
//构造Request对象
BulkDeleteRequest bulkDeleteRequest = new BulkDeleteRequest();
//批量删除
bulkDeleteRequest.JobName = "批量删除";
//批量作业開始时间
bulkDeleteRequest.StartDateTime = DateTime.Now;
//作业完毕之后是否发生邮件
bulkDeleteRequest.SendEmailNotification = false;
bulkDeleteRequest.ToRecipients = new Guid[] { };
bulkDeleteRequest.CCRecipients = new Guid[] { };
//设置作业执行的时间间隔
bulkDeleteRequest.RecurrencePattern = String.Empty;
//删除的记录
QueryExpression query = new QueryExpression();
//实体的名称
query.EntityName = "new_customlog";
query.Criteria.AddCondition(new ConditionExpression("statecode",ConditionOperator.Equal,0));
bulkDeleteRequest.QuerySet = new QueryExpression[] { query };
//提交作业,系统将运行
BulkDeleteResponse _bulkDeleteResponse = (BulkDeleteResponse)service.Execute(bulkDeleteRequest);
System.Console.WriteLine("作业已经提交,系统正在运行!");
CheckSuccess(_bulkDeleteResponse,service);
}
/// <summary>
/// 检測作业是否完毕
/// </summary>
private void CheckSuccess(BulkDeleteResponse _bulkDeleteResponse,IOrganizationService service)
{
QueryByAttribute bulkQuery = new QueryByAttribute();
bulkQuery.ColumnSet = new ColumnSet("statecode", "successcount", "failurecount");
bulkQuery.EntityName = "bulkdeleteoperation";
bulkQuery.Attributes.Add("asyncoperationid");
bulkQuery.Values.Add(_bulkDeleteResponse.JobId);
EntityCollection entityCollection = service.RetrieveMultiple(bulkQuery);
int secondsTicker = 60;
Entity createdBulkDeleteOperation = null;
#region
while (secondsTicker > 0)
{
if (entityCollection != null && entityCollection.Entities.Count > 0)
{
createdBulkDeleteOperation = entityCollection.Entities[0];
int statecode = ((OptionSetValue)createdBulkDeleteOperation["statecode"]).Value;
BulkDeleteOperationState optionState =
((BulkDeleteOperationState)(System.Enum.ToObject(typeof(BulkDeleteOperationState), statecode)));
if (optionState != BulkDeleteOperationState.Completed)
{
//先暂停
System.Threading.Thread.Sleep(1000);
secondsTicker--;
// 再次查询
entityCollection = service.RetrieveMultiple(bulkQuery);
System.Console.WriteLine("系统正在运行!");
}
else
{
//作业运行完成。退出循环
secondsTicker = 0;
}
}
else
{
//先暂停
System.Threading.Thread.Sleep(1000);
secondsTicker--;
// 再次查询
entityCollection = service.RetrieveMultiple(bulkQuery);
System.Console.WriteLine("系统正在运行!");
}
}
#endregion
if (createdBulkDeleteOperation != null)
{
int statecode = ((OptionSetValue)createdBulkDeleteOperation["statecode"]).Value;
BulkDeleteOperationState optionState =
((BulkDeleteOperationState)(System.Enum.ToObject(typeof(BulkDeleteOperationState), statecode)));
if (optionState == BulkDeleteOperationState.Completed)
{
int successcount = Convert.ToInt32(createdBulkDeleteOperation["successcount"]);
int failurecount = Convert.ToInt32(createdBulkDeleteOperation["failurecount"]);
System.Console.WriteLine("系统运行完成!成功记录数:{0},失败记录数:{1}", successcount, failurecount);
}
else
{
System.Console.WriteLine("系统还在运行!");
}
}
}
}
[System.Runtime.Serialization.DataContractAttribute()]
public enum BulkDeleteOperationState
{
[System.Runtime.Serialization.EnumMemberAttribute()]
Ready = 0,
[System.Runtime.Serialization.EnumMemberAttribute()]
Suspended = 1,
[System.Runtime.Serialization.EnumMemberAttribute()]
Locked = 2,
[System.Runtime.Serialization.EnumMemberAttribute()]
Completed = 3,
}