Notifications
Announcements
No record found.
Hi,
How can I delete MSCRM audit log or audit log history through C# custom code and what user role will be required for doing that?
*This post is locked for comments
Hi MSCRM Developer,
You could enable ‘Delete Audit Record Change history’ role to control user delete Audit logs.
[View:https://www.inogic.com/blog/2018/05/deleting-audit-log-in-dynamics-365/amp/]
In addition, there is a DeleteAuditDataRequest in the SDK:
[View:https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg327533(v=crm.8)]
Overall Retrieve and delete the history of audited data changes instruction:
[View:https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/retrieve-and-delete-the-history-of-audited-data-changes]
As I know, you can only delete all audit log records up until a specific date using that request.
Here’s a thread for your reference:
[View:https://community.dynamics.com/crm/f/117/t/236432]
Hope the above could help.
Regards
Johnny
You can use DeleteAuditDataRequest class and for privileges you can check docs.microsoft.com/.../gg334799%28v%3dcrm.8%29
Thanks,
Ridhima
Sample code for quick help,
// Get the list of audit partitions. RetrieveAuditPartitionListResponse partitionRequest = (RetrieveAuditPartitionListResponse)_service.Execute(new RetrieveAuditPartitionListRequest()); AuditPartitionDetailCollection partitions = partitionRequest.AuditPartitionDetailCollection; // Create a delete request with an end date earlier than possible. DeleteAuditDataRequest deleteRequest = new DeleteAuditDataRequest(); deleteRequest.EndDate = new DateTime(2000, 1, 1); // Check if partitions are not supported as is the case with SQL Server Standard edition. if (partitions.IsLogicalCollection) { // Delete all audit records created up until now. deleteRequest.EndDate = DateTime.Now; } // Otherwise, delete all partitions that are older than the current partition. // Hint: The partitions in the collection are returned in sorted order where the // partition with the oldest end date is at index 0. else { for (int n = partitions.Count - 1; n >= 0; --n) { if (partitions[n].EndDate<DateTime.Now && partitions[n].EndDate>deleteRequest.EndDate) { deleteRequest.EndDate=(DateTime)partitions[n].EndDate; break; } } } // Delete the audit records. if (deleteRequest.EndDate != new DateTime(2000, 1, 1)) { _service.Execute(deleteRequest); Console.WriteLine("Audit records have been deleted."); } else Console.WriteLine("There were no audit records that could be deleted.");
Thanks! Now, I am able to delete bulk records of my last audit partition but it restricts me to delete current or running quarter or partition records.
How can i delete particular records using DeleteRecordChangeHistoryRequest. can you please provide me the C# code using DeleteRecordChangeHistoryRequest?
You can use the Dynamics Workflow Tools utility to delete the record history. There is a new step added recently.
Check this: github.com/.../DeleteRecordAuditHistory.md
Hope this helps.
Thanks for your response.
But I want to achieve this through C# custom code using SDK.
bump into this, I was able to execute this function with this.
var DeleteAudit = new DeleteRecordChangeHistoryRequest();
//-- Set entity to delete - workaround as CRM does not allow to delete Audit record directly
DeleteAudit.Target = (EntityReference)record.Attributes["objectid"]; //this is a contact record
if(EntityLogReference.Id != Guid.Empty)
{
_service.Execute(DeleteAudit); //delete all audit history
}
This code runs in a loop of all Audit records and I am filtering it with specific entity w/c is the contact.
Prerequisite reference Microsoft.Crm.Sdk.Proxy (v9.0)
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
SA-08121319-0 4
Calum MacFarlane 4
Alex Fun Wei Jie 2