Retrieving Audit Data for a Specific Entity in D365 CE Using C#

In Dynamics 365 CE, the audit feature plays a pivotal role in tracking data changes for compliance, security, and analytical insights. Sometimes we need to get Audit data based operation and specific entity. This blog will provide you sample code to get data from Audit using C#.

Understanding Dynamics 365 Audit History
Dynamics 365 CE’s Audit History provides a detailed record of changes made to data within the system. This includes alterations to records, attribute changes, and more. Leveraging this information programmatically allows organizations to maintain a comprehensive record of actions, enhancing transparency and accountability. Audit entity store data based on the different operations which includes:

Create: Records the creation of an audit record.
Update: Captures changes to an existing audit record.
Delete: Logs the deletion of an audit record.

We can see complete list of operations here

We can go to Settings > Select Advanced Settings > Settings and select Auditing option to review auditing settings in Dynamics 365 CE, but sometimes we need to interact with audit entity using code for example let’s say we want to retrieve audit record for a specific entity for a specific operation. To implement this requirement we can write simple FetchXml like below

private Guid GetAuditRecordGUID(Guid entityid)
 string auditFetch = @"<fetch>
                                  <entity name='audit' >
                                    <filter type='and' >
                                      <condition attribute='operation' operator='eq' value='3' />
                                      <condition attribute='createdon' operator='today' />
                                      <condition attribute='objectid' operator='eq' value='{0}' />

            auditFetch = String.Format(auditFetch, entityid);
return service.RetrieveMultiple(new FetchExpression(auditFetch)).Entities.FirstOrDefault().Id;

In above code I am using FetchXML query to get audit history of entity based on the following three condtions:

  <condition attribute='operation' operator='eq' value='3' />

Above condition is used to fetch audit history for the delete operation as value of delete operation is 3.

  <condition attribute='createdon' operator='today' />

I just want to get data which is deleted today so adding condition to get the record which is created today in audit entity.

  <condition attribute='objectid' operator='eq' value='{0}' />

In above condition objectid represent the entity for which audit data is created, so I want to fetch data for specific entity record. While fetch data from audit history, keep in mind we may not get these record immediately so make sure to design your solution accordingly.

By using above code we can read audit history data for specific operations on a particular entity in Dynamics 365 CE.

Hope it will help someone !!
Keep learning and Keep Sharing !!

Leave a Reply

Your email address will not be published. Required fields are marked *