【问题标题】:Query Dataverse audit logs from Azure Function从 Azure Function 查询 Dataverse 审核日志
【发布时间】:2023-03-20 03:47:02
【问题描述】:

我是新手,请见谅

我们有一个需求Every X days, query changes made in tables/records in dataverse and send email with these changes

根据我的研究,这可以通过 Power Automate 完成,因此每 7 天运行一次 HTTP 触发器,该触发器触发一个 C# 函数,该函数查询审核日志并发送一封电子邮件,其中包含已更改的事物表。

我的测试环境中有审计日志,它显示了对表所做的更改,这只是我不确定的其余部分。

我遇到的主要问题是如何从 C# Azure Function 应用程序查询 dataverse 审计日志?

【问题讨论】:

    标签: c# azure-functions audit-logging dataverse


    【解决方案1】:

    使用 Azure 函数

    您可以使用 audits 实体集名称对 Dataverse Web API 执行 HTTP 请求。该请求类似于:

    GET /api/data/v9.2/audits HTTP/1.1
    Host: {organizationUniqueName}.{region}.dynamics.com
    Authorization: Bearer eyJ0eXAi...
    

    注意,您需要在请求中提供不记名身份验证令牌(有关如何执行此操作的详细信息,请参阅Use OAuth authentication with Microsoft Dataverse。有关查询 Dataverse Web API 的综合指南,请参阅Query data using the Web API

    或者,您可以使用以下任一 NuGet 包来使用 C# 查询 Dataverse。

    1. Microsoft.CrmSdk.CoreAssemblies(仅适用于 Azure Function runtime 1.x)
    2. Microsoft.PowerPlatform.Dataverse.Client(公开预览。适用于运行时 2.x+)

    无论您选择使用哪个包,C# 代码看起来或多或少都相同,除了ServiceClient 适用于第二个 NuGet 包,CrmServiceClient 适用于第一个包。

    var client = new CrmServiceClient(connectionString);
    
    var auditLogsQuery =
        @"<fetch top='50' >
            <entity name='audit' >
                <filter>
                <condition attribute='operation' operator='eq' value='1' />
                <condition attribute='createdon' operator='last-x-hours' value='1' />
                </filter>
            </entity>
        </fetch>";
    
    EntityCollection auditLogs = client.RetrieveMultiple(new FetchExpression(auditLogsQuery));
    

    使用 Power Automate

    鉴于您提到了使用 Power Automate,我建议您考虑使用它来满足您的要求。通过内置的 Dataverse 连接器查询 Dataverse 中的数据是 Power Automate 的原生功能。

    假设您有下面的Fetch XML 查询来拉回操作为Create (1) 并且操作在过去一小时内执行的所有审计日志。

    提示:您可以使用XrmToolBox 中的FetchXML Builder 插件来编写这些查询。不支持在高级查找中查询审计日志。

    <fetch top="50" >
      <entity name="audit" >
        <filter>
          <condition attribute="operation" operator="eq" value="1" />
          <condition attribute="createdon" operator="last-x-hours" value="1" />
        </filter>
      </entity>
    </fetch>
    

    使用 Dataverse List rows 操作,您可以指定要查询 Audits 表并在操作的 Fetch Xml Query 属性中提供上述 Fetch XML 查询。请参阅下面的示例:

    此查询的结果现在可在整个 Power Automate 云流程中使用。例如,这里我使用Compose 操作为返回的审计日志写出OperationRecord TypeRecord IDChanged Field

    【讨论】:

      猜你喜欢
      • 2019-10-02
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      相关资源
      最近更新 更多