博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
crm查询和删除审核历史记录
阅读量:5905 次
发布时间:2019-06-19

本文共 4164 字,大约阅读时间需要 13 分钟。

    using System;

    using System.Linq;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Messages;
    using System.Collections.Generic;

    /// <summary>

    /// 审核历史记录
    /// </summary>
    public class AuditHistory
    {
        /// <summary>
        /// 检索对特定属性所做的所有更改
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="Target">记录对象</param>
        /// <param name="attrName">字段名称</param>
        /// 特别说明:字段必须开启审核
        public void RetrieveAttributeHistory(IOrganizationService service, EntityReference target,string attrName)
        {
             RetrieveAttributeChangeHistoryRequest request = new RetrieveAttributeChangeHistoryRequest();
             request.AttributeLogicalName = attrName;
             request.Target = target;
             request.PagingInfo = new PagingInfo() { Count = 1000,PageNumber = 1 };
             RetrieveAttributeChangeHistoryResponse response = (RetrieveAttributeChangeHistoryResponse)service.Execute(request);
             //结果
             AuditDetailCollection auditDetailCollection = response.AuditDetailCollection;
        }

        /// <summary>

        /// 索对特定实体所做的所有更改
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="target">目标记录</param>
        public void RetrieveEntityHistory(IOrganizationService service, EntityReference target)
        {
            RetrieveRecordChangeHistoryRequest request = new RetrieveRecordChangeHistoryRequest();
            request.Target = target;
            request.PagingInfo = new PagingInfo() { Count = 1000,PageNumber = 1 };
            RetrieveRecordChangeHistoryResponse response = (RetrieveRecordChangeHistoryResponse)service.Execute(request);
            //结果
            AuditDetailCollection auditDetailCollection = response.AuditDetailCollection;
        }

        /// <summary>

        /// 删除包括在给定结束日期之前创建的审核数据的全部分区
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="endDate">结束日期</param>
        public void DeleteAudit(IOrganizationService service, DateTime endDate)
        {
            DeleteAuditDataRequest request = new DeleteAuditDataRequest();
            request.EndDate = endDate;
            DeleteAuditDataResponse response = (DeleteAuditDataResponse)service.Execute(request);
        }

        /// <summary>

        /// 删除审核记录
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="auditDetailCollection">记录集合</param>
        public void DeleteAudit(IOrganizationService service, AuditDetailCollection auditDetailCollection)
        {
            if (auditDetailCollection.AuditDetails != null && auditDetailCollection.AuditDetails.Count > 0)
            {
                ExecuteMultiple multiple = new ExecuteMultiple();
                multiple.AddDelete(auditDetailCollection.AuditDetails.Select(a => a.AuditRecord));
                multiple.Execute(service);
            }
        }

 

      

/// <summary>

    /// 批量操作
    /// </summary>
    public class ExecuteMultiple
    {
        private int len = 1500;
        private List<ExecuteMultipleRequest> list = new List<ExecuteMultipleRequest>();

        public ExecuteMultiple()

        {
            New();
        }

        public void New()

        {
            ExecuteMultipleRequest multiple = new ExecuteMultipleRequest()
            {
                Settings = new ExecuteMultipleSettings()
                {
                    ContinueOnError = false,
                    ReturnResponses = true
                },
                Requests = new OrganizationRequestCollection()
            };
            list.Add(multiple);
        }

        public void Add(OrganizationRequest request)

        {
            ExecuteMultipleRequest multiple = list.LastOrDefault();
            if (multiple.Requests.Count >= len)
            {
                New();
                multiple = list.LastOrDefault();
                multiple.Requests.Add(request);
            }
            else
            {
                multiple.Requests.Add(request);
            }
        }

        public void AddCreate(Entity en)

        {
            CreateRequest request = new CreateRequest() { Target = en };
            Add(request);
        }

        public void AddUpdate(Entity en)

        {
            UpdateRequest request = new UpdateRequest() { Target = en };
            Add(request);
        }

        public void AddDelete(EntityReference er)

        {
            DeleteRequest request = new DeleteRequest() { Target = er };
            Add(request);
        }

        public void AddDelete(Entity en)

        {
            AddDelete(new EntityReference() { LogicalName = en.LogicalName, Id = en.Id });
        }

        public void AddDelete(EntityCollection ec)

        {
            if (ec != null && ec.Entities.Count > 0)
            {
                AddDelete(ec.Entities);
            }
        }

        public void AddDelete(DataCollection<Entity> list)

        {
            if (list != null && list.Count > 0)
            {
                foreach (Entity en in list)
                {
                    AddDelete(en);
                }
            }
        }

        public void AddDelete(IEnumerable<Entity> list)

        {
            if (list != null && list.Count() > 0)
            {
                foreach (Entity en in list)
                {
                    AddDelete(en);
                }
            }
        }

        public void Execute(IOrganizationService service)

            {
                foreach (ExecuteMultipleRequest request in list)
                {
                    service.Execute(request);
                }
            }
    }

 

         
    }

转载地址:http://qbcpx.baihongyu.com/

你可能感兴趣的文章
禁止server 2008域端口的脚本
查看>>
数据结构图之二(最小生成树--普里姆算法)
查看>>
HTML输出 一 控制列背景颜色
查看>>
Redis for Windows(C#缓存)配置文件详解
查看>>
回忆2013年的点点滴滴(各个方面)
查看>>
ASP.NET MVC 4使用PagedList.Mvc分页
查看>>
HDOJ 2066 floyed优化算法
查看>>
window.onscroll
查看>>
开发常用动画收集
查看>>
nginx js、css多个请求合并为一个请求(concat模块)
查看>>
mybatis实战教程(mybatis in action)之五:与spring3集成
查看>>
解决浏览器Adobe Flash Player不是最新版本问题
查看>>
SQLite 约束
查看>>
Python爬虫学习——使用Cookie登录新浪微博
查看>>
linux配置网络
查看>>
vsftp 500 OOPS: cannot change directory:/home/xyp
查看>>
MVC ---- EF的安装于卸载
查看>>
WebRTC 学习之 概念总结
查看>>
Java对ad操作
查看>>
unity与android交互总结
查看>>