首页 > 代码库 > crm2011查询审核记录

crm2011查询审核记录

    using System;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Crm.Sdk.Messages;

    /// <summary>
    /// 查询审核记录
    /// </summary>
    public class RetrieveRecordChangeHistoryRequestHelper
    {
        public void Retrieve(IOrganizationService service,string entityName,Guid id)
        {
            RetrieveRecordChangeHistoryRequest changeRequest = new RetrieveRecordChangeHistoryRequest();
            changeRequest.Target = new EntityReference() { LogicalName = entityName,Id = id };

            RetrieveRecordChangeHistoryResponse changeResponse = (RetrieveRecordChangeHistoryResponse)service.Execute(changeRequest);

            AuditDetailCollection details = changeResponse.AuditDetailCollection;
            if (details != null && details.AuditDetails.Count > 0)
            {
                foreach (AuditDetail detail in details.AuditDetails)
                {
                    System.Console.WriteLine(detail.GetType());
                    DisplayEntity(detail.AuditRecord, "AuditRecord");
                    if (detail.GetType() == typeof(AttributeAuditDetail))
                    {
                        AttributeAuditDetail attrDetail = (AttributeAuditDetail)detail;
                        foreach (string item in attrDetail.InvalidNewValueAttributes)
                        {
                            System.Console.WriteLine(item);
                        }
                        DisplayEntity(attrDetail.NewValue, "NewValue");
                        DisplayEntity(attrDetail.OldValue, "OldValue");
                    }
                    if (detail.GetType() == typeof(RelationshipAuditDetail))
                    {
                        RelationshipAuditDetail shipDetail = (RelationshipAuditDetail)detail;
                        System.Console.WriteLine("RelationshipName" + shipDetail.RelationshipName);
                        if (shipDetail.TargetRecords != null && shipDetail.TargetRecords.Count > 0)
                        {
                           foreach(EntityReference item in shipDetail.TargetRecords)
                           {
                               System.Console.WriteLine("LogicalName: " + item.LogicalName + ",Name: " + item.Name);
                           }
                        }
                    }
                }
            }

        }

        public void DisplayEntity(Entity entity,string label)
        {
            System.Console.WriteLine("display" + label + "start!");
            var keyArray = entity.Attributes.Keys;
            foreach (string name in keyArray)
            {
                System.Console.WriteLine("attributeName: " + name + ",attributeValue: " + entity.Attributes[name]);
            }
            System.Console.WriteLine("display" + label + "end!");
        }
    }