rest - 在展开查询中选择之前进行 OData 过滤

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

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

我的问题如下:

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

{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')

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

但是当我需要包含 Targets 时,我收到一条错误消息
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 属性中调用时这个顺序不同。

最佳答案

为了获得查找实体,您可能会更幸运地执行 $expandManyToOneRelationships而不是 Attributes并获得 ReferencedEntity值(value)。

ManyToOne

这样的事情应该工作:.../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(ReferencingAttribute,ReferencedEntity))/$entity","LogicalName":"account","MetadataId":"70816501-edb9-4740-a16c-6a5efbc05d84","ManyToOneRelationships":[{"ReferencingAttribute":"msdyn_accountkpiid","ReferencedEntity":"msdyn_accountkpiitem","MetadataId":"2a712c96-09b1-e811-a842-000d3a33bdbd"},{"ReferencingAttribute":"preferredequipmentid","ReferencedEntity":"equipment","MetadataId":"b4b462b5-ee78-467d-a97a-45264d234816"},{"ReferencingAttribute":"primarycontactid","ReferencedEntity":"contact","MetadataId":"410707b1-9554-4cd9-8437-6608b1802904"},{"ReferencingAttribute":"masterid","ReferencedEntity":"account","MetadataId":"51fa4af7-93d0-4f06-8949-38a0036ddc64"},{"ReferencingAttribute":"preferredsystemuserid","ReferencedEntity":"systemuser","MetadataId":"a6b48e23-fada-4b7f-8655-530bba050765"},{"ReferencingAttribute":"createdbyexternalparty","ReferencedEntity":"externalparty","MetadataId":"9967fe7d-84ee-4a26-9ad7-a8fdbdfa2316"},{"ReferencingAttribute":"modifiedby","ReferencedEntity":"systemuser","MetadataId":"8be02a9d-0776-4c76-b35f-1c92dd791d9e"},{"ReferencingAttribute":"parentaccountid","ReferencedEntity":"account","MetadataId":"57511732-b553-4cfb-bcf2-d280f9f8c6f1"},{"ReferencingAttribute":"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)
它抛出一个错误:

"Query option '$expand' was specified more than once, but it must be specified at most once."

关于rest - 在展开查询中选择之前进行 OData 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54218058/

相关文章:

java - Spring Boot 中的 @PathVariable 在 URL 中带有斜杠

c# - 是否可以使用带符号的 REST url 参数?

dynamics-crm - Microsoft Daynamics WebApi - 导航属性 'bpf_quoteid' 没有扩展值,也没有 'odata.bind' 属性注释

javascript - 处理获取 xml 中的特殊字符

asp.net - 具有Subsonic 3的OData(WCF数据服务)

rest - 本地 IP 地址的 HTTPS

Spring MVC Spring 安全性和错误处理

java - 使用 Apache Olingo 指定实体类型

angular - @ 符号的 Angular 2 模板插值的 odata 响应错误

json - 以 JSON 格式查询 Microsoft Dynamics NAV 2013 Odata 服务