码迷,mamicode.com
首页 > 其他好文 > 详细

crm2011创建批量删除作业

时间:2014-06-15 08:29:53      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:crm2011创建批量删除作业   crm批量删除   crm自定义开发   crm2011   crm   

    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,
    }

crm2011创建批量删除作业,布布扣,bubuko.com

crm2011创建批量删除作业

标签:crm2011创建批量删除作业   crm批量删除   crm自定义开发   crm2011   crm   

原文地址:http://blog.csdn.net/y_f123/article/details/30221859

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!