【问题标题】:OData filtering before selecting inside an expand query在展开查询中选择之前的 OData 过滤
【发布时间】:2019-01-16 13:25:14
【问题描述】:

我正在 Dynamics 365 中编写元数据查询,但我认为这个问题对于任何 OData 查询都是通用的。

我的问题如下:

在 Dynamics 中,我们可以在实体中拥有多种类型的字段,例如字符串、布尔值或查找。我想编写一个查询,返回查找所寻址的实体类型。

{url}/api/data/v9.0/EntityDefinitions(LogicalName='Account')
?$select=
    LogicalName
&$expand=
    Attributes(
        $select=
            LogicalName,
            AttributeType,
            Targets; -- Problematic property
        $filter=
      AttributeType+eq+Microsoft.Dynamics.CRM.AttributeTypeCode'Lookup')

如果我在此查询中不包括对 Targets 的选择,我会得到正确的结果,即它们的 AttributeType 为 Lookup 的所有属性。

但是当我要求包含目标时,我收到一条错误消息

Could not find a property named 'Targets' on type 'Microsoft.Dynamics.CRM.AttributeMetadata

因为 Target 属性只存在于 Lookup 类型的那些 arrribute 上,因此从字符串属性中选择此列将失败并抛出此错误。

有没有办法在选择列之前先过滤查找?我发现here的评估顺序是

$filter、$inlinecount、$orderby、$skiptoken、$skip、$top、$expand、$select、$format

这正是我所需要的,除了我认为在 $expand 属性中调用时这个顺序不一样。

【问题讨论】:

    标签: rest dynamics-crm odata query-string odata-v4


    【解决方案1】:

    为了获得查找实体,您可能会在ManyToOneRelationships 上执行$expand 而不是Attributes 并获得ReferencedEntity 值会更好。

    这样的事情应该可以工作:
    .../api/data/v9.1/EntityDefinitions(LogicalName='account') ?$select=LogicalName &$expand=ManyToOneRelationships($select=ReferencingAttribute,ReferencedEntity)

    结果子集:

    {"@odata.context":"https://myOrg.api.crm.dynamics.com/api/data/v9.1/$metadata#EntityDefinitions(LogicalName,ManyToOneRelationships(ReferencedEntity))/$entity","LogicalName":"account","MetadataId":"70816501-edb9 -4740-a16c-6a5efbc05d84","ManyToOneRelationships":[{"ReferenceAttribute":"msdyn_accountkpiid","ReferencedEntity":"msdyn_accountkpiitem","MetadataId":"2a712c96-09b1-e811-a842-000d3a33bdbd"},{"ReferencingAttribute ":"preferredequipmentid","ReferencedEntity":"equipment","MetadataId":"b4b462b5-ee78-467d-a97a-45264d234816"},{"ReferenceAttribute":"primarycontactid","ReferencedEntity":"contact","MetadataId ":"410707b1-9554-4cd9-8437-6608b1802904"},{"ReferenceAttribute":"masterid","ReferencedEntity":"account","MetadataId":"51fa4af7-93d0-4f06-8949-38a0036ddc64"},{ "ReferenceAttribute":"preferredsystemuserid","ReferencedEntity":"systemuser","MetadataId":"a6b48e23-fada-4b7f-8655-530bba050765"},{"ReferenceAttribute":"createdbyexternalparty","ReferencedEntity":"externalparty", “元数据 ID”:“9967fe7d- 84ee-4a26-9ad7-a8fdbdfa2316"},{"ReferenceAttribute":"modifiedby","ReferencedEntity":"systemuser","MetadataId":"8be02a9d-0776-4c76-b35f-1c92dd791d9e"},{"ReferencingAttribute":" parentaccountid","ReferencedEntity":"account","MetadataId":"57511732-b553-4cfb-bcf2-d280f9f8c6f1"},{"ReferenceAttribute":"entityimageid","ReferencedEntity":"imagedescriptor","MetadataId":" 5b4942d5-1fcd-49ca-91c0-2737f5f104f3"},

    另外,作为参考,我尝试在属性和目标上执行$expand
    ../api/data/v9.1/EntityDefinitions(LogicalName='account')?$select=LogicalName&$expand=Attributes($filter=AttributeType+eq+Microsoft.Dynamics.CRM.AttributeTypeCode%27Lookup%27&$expand=Targets)

    它抛出一个错误:

    "查询选项 '$expand' 被多次指定,但必须是 最多指定一次。”

    【讨论】:

    • 您好,感谢您的回复。元数据的扩展有很大帮助。我曾尝试专门调用元数据端点,但我没有想到您可以扩展关系而不是属性。这个查询成功了。也供参考:我正在编写一些东西来获取程序需要映射的所有数据,这意味着从查找的查找中下载值。我必须实现逻辑将其拆分为多个调用,特别是因为 Dynamics 当时仅限于一个扩展调用
    【解决方案2】:

    如果您用逗号分隔 Attributes 和 ManyToOneRelationships,则可以在同一个调用中展开它们。

    GET https://{{baseUrl}}/api/data/v9.1/EntityDefinitions(LogicalName='account')?$select=LogicalName,EntitySetName&$expand=Attributes($select=LogicalName),ManyToOneRelationships($select=ReferencingAttribute,ReferencedEntity)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多