web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Friyank’s blog / How to get Audit Details fr...

How to get Audit Details from MS CRM (C#.NET)

Friyank Profile Picture Friyank 944

In CRM you can see Audit Details as below 01

But what about getting it in Code and having control over it.

Below Code will have following object in it.

02


using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Configuration;

namespace AuditableEntityBlog
{
 class Program
 {
 static void Main(string[] args)
 {
 CrmServiceClient crmServiceClientObj = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CrmOnlineStringFromAppConfig"].ConnectionString);
 if (!crmServiceClientObj.IsReady)
 Console.WriteLine("No Connection was Made.");
 Console.WriteLine("Connected");
 string EntitySchemaName = "account",AttributeSchemaName = "name";
 Guid RecordId = new Guid("6C8A7CF2-64E9-E711-A952-000D3A328908");
 try
 {
 RetrieveAttributeChangeHistoryRequest attributeChangeHistoryRequest = new RetrieveAttributeChangeHistoryRequest
 {
 Target = new EntityReference(EntitySchemaName, RecordId),
 AttributeLogicalName = AttributeSchemaName
 };

RetrieveAttributeChangeHistoryResponse attributeChangeHistoryResponse =
 (RetrieveAttributeChangeHistoryResponse)crmServiceClientObj.Execute(attributeChangeHistoryRequest);

foreach (var EachEditRecord in attributeChangeHistoryResponse.AuditDetailCollection.AuditDetails)
 {
 AttributeAuditDetail attributeDetail = (AttributeAuditDetail)EachEditRecord;
 }

}
 catch (Exception e)
 {
 Console.WriteLine("Failed to fetch data. \nReason {0}", e.Message);
 }
 Console.ReadLine();
 }
 }
}

In the Above Code i have used RetrieveAttributeChangeHistoryRequest to get Audit for specific Entity, Specific Record and also Specific Attribute.
Then breaking RetrieveAttributeChangeHistoryResponse further into AuditDetailCollection and then into AuditDetails returns me AuditDetails Object but this object have no OldValue and NewValue object, so to fetch oldValue and NewValue Entity we need to type cast it into AttributeAuditDetail.

AuditDetails  will have basic information which is called Primary data of audit.
Type Casting it into AttributeAuditDetail will give OldValue and NewValue object which is called Secondary data of audit

Hope it helps!


Comments

*This post is locked for comments