删除两个日期之间的Dynamics CRM审核日志

时间:2020-03-27 14:39:23

标签: c# plugins dynamics-crm dynamics-crm-online

我想问一下是否有想法在两个日期之间删除Dynamics CRM Online中的审核日志

1 个答案:

答案 0 :(得分:0)

Documentation说:

删除日期范围内的更改历史记录

您可以使用DeleteAuditDataRequest请求删除日期范围内的审核记录。审计数据记录从最早的到最新的依次删除。根据您的Common Data Service服务器使用的Microsoft SQL Server版本,此请求的功能略有不同。公共数据服务使用SQL Server的企业版。

如果您的Common Data Service服务器使用的SQL Server标准版不支持数据库分区功能,则DeleteAuditDataRequest请求将删除在EndDate属性中指定的截止日期之前创建的所有审核记录。 如果您的Common Data Service服务器使用支持分区的SQL Server企业版,则DeleteAuditDataRequest请求将删除终止日期早于EndDate属性中指定的日期的那些分区中的所有审核数据。任何空分区也将被删除。但是,使用此请求或任何其他请求都不能删除当前(活动)分区或该活动分区中的审核记录。

Common Data Service平台每年每季度自动创建一个新分区。此功能是不可配置的,无法更改。您可以使用RetrieveAuditPartitionListRequest请求获取分区列表。如果任何分区的结束日期晚于当前日期,则无法删除该分区或其中的任何审核记录。

我们不能在两个日期之间精确删除,但是您可以通过仅早于某个结束日期的分区来删除审核数据。

Code sample

// Get the list of audit partitions.
          var partitionRequest =(RetrieveAuditPartitionListResponse)svc.Execute(new RetrieveAuditPartitionListRequest());
            AuditPartitionDetailCollection partitions = partitionRequest.AuditPartitionDetailCollection;

// Create a delete request with an end date earlier than possible.
         var 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))
    {
       svc.Execute(deleteRequest);
        Console.WriteLine("Audit records have been deleted.");
    }
    else
    Console.WriteLine("There were no audit records that could be deleted.");

在网络api中,您可以使用DeleteAuditData操作。