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

Introduction

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 on operations and specific entities. This blog will provide you with a sample code to get data from Audit using C#.

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 a complete list of operations here.

We can go to Settings > Select Advanced Settings > Settings and select the Auditing option to review auditing settings in Dynamics 365 CE, but sometimes, we need to interact with the audited entity using code. For example, let’s say we want to retrieve audit records for a specific entity for a specific operation. To implement this requirement, we can write the 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}' />
                </filter>
            </entity>
        </fetch>";

    auditFetch = string.Format(auditFetch, entityid);
    return service.RetrieveMultiple(new FetchExpression(auditFetch)).Entities.FirstOrDefault()?.Id ?? Guid.Empty;
}

In the above code, I am using the FetchXML query to get the audit history of an entity based on the following three conditions.

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

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

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

I just want to get data that is deleted today, so I am adding a condition to get the record that is created today in the audit entity.

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

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

Summary

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

I hope it will help someone !!

Keep learning and Keep Sharing !!


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.