dynamic crm 批量删除数据

代码来源:BulkDeleteRequest 类

进行了简单改写,实现了批量删除功能,直接上代码:

            // Create a condition for a bulk delete request.
            // NOTE: This sample uses very specific queries for deleting records
            // that have been manually exported in order to free space.
            //QueryExpression opportunitiesQuery = BuildOpportunityQuery();
            //上面一句代码其实就是构建一个查询语句,我以以下的方法直接替代
            QueryExpression opportunitiesQuery = new QueryExpression();
            opportunitiesQuery.EntityName = "";//要查询的实体
            opportunitiesQuery.ColumnSet = new ColumnSet("");//要查询的字段
            opportunitiesQuery.Criteria.FilterOperator = LogicalOperator.And;//查询条件
            opportunitiesQuery.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));

            // Create the bulk delete request.
            BulkDeleteRequest bulkDeleteRequest = new BulkDeleteRequest();

            // Set the request properties.
            bulkDeleteRequest.JobName = "Backup Bulk Delete";

            // Querying activities
            bulkDeleteRequest.QuerySet = new QueryExpression[]
            {
                opportunitiesQuery,
                //以下的代码应该是查询相关连的记录
                //BuildActivityQuery(Task.EntityLogicalName),
                //BuildActivityQuery(Fax.EntityLogicalName),
                //BuildActivityQuery(PhoneCall.EntityLogicalName),
                //BuildActivityQuery(Email.EntityLogicalName),
                //BuildActivityQuery(Letter.EntityLogicalName),
                //BuildActivityQuery(Appointment.EntityLogicalName),
                //BuildActivityQuery(ServiceAppointment.EntityLogicalName),
                //BuildActivityQuery(CampaignResponse.EntityLogicalName),
                //BuildActivityQuery(RecurringAppointmentMaster.EntityLogicalName)
            };

            // Set the start time for the bulk delete.
            bulkDeleteRequest.StartDateTime = DateTime.Now;

            // Set the required recurrence pattern.
            bulkDeleteRequest.RecurrencePattern = String.Empty;

            // Set email activity properties.
            bulkDeleteRequest.SendEmailNotification = false;
            //输入当前用户的id值,经过实测虽然不需要发送邮件但这个字段还是必填的
            //ToRecipients的属性解释如下,注意Required
            // Gets or sets an array of IDs for the system users(users) who are listed in the
            // To box of an email notification. Required.
            bulkDeleteRequest.ToRecipients = new Guid[] { currentUserId };
            bulkDeleteRequest.CCRecipients = new Guid[] { };

            // Submit the bulk delete job.
            // NOTE: Because this is an asynchronous operation, the response will be immediate.
            //也可以简单一点,不需要返回值
            //_bulkDeleteResponse =
            //   (BulkDeleteResponse)_serviceProxy.Execute(bulkDeleteRequest);
            service.Execute(bulkDeleteRequest);
            Console.WriteLine("The bulk delete operation has been requested.");

 

你可能感兴趣的:(dynamic,crm)