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); } } }}