仅获取 OData 中实体的 属性 个定义

Getting only property definitions of entity in OData

我正在开发一个需要与 OData 服务(确切地说是 Microsoft Dynamics CRM)通信的应用程序。我有一个要求,我只需要知道实体具有的所有属性。

例如

[Organization URI]/api/data/v8.1/contacts

return 所有联系人,但我只想要 属性 联系人定义。

现在 [Organization URI]/api/data/v8.1/contacts return 具有值的 JSON,但是我正在寻找的是联系人实体的某种模式。它应该 return 我知道它拥有的所有属性(例如 firstnamelastname)以及可能的属性类型。

我尝试使用 $metadata 但没有成功。是否可以仅获取有关实体的信息?

如有任何帮助,我们将不胜感激。

如果您正在使用 Web Api(看起来像您),那么您可以检索 EntityMetadata,其中有 AttributeMetadata 的导航 属性,将描述属性(又名;字段、属性)。

[Organization URI]/api/data/v8.1/EntityDefinitions

Use the Web API with CRM metadata

例如:GET [Organization URI]/api/data/v8.1/EntityDefinitions?$select=DisplayName,IsKnowledgeManagementEnabled,EntitySetName&$filter=SchemaName eq 'Contact' HTTP/1.1

更多示例位于 Query Metadata using the Web API

参见Query Metadata using the Web API。上一个答案很接近,但您需要 $expand=Attributes 来获取属性列表。此外,它还会获取您要在联系人上过滤的所有实体。

GET [Organization URI]/api/data/v8.1/EntityDefinitions?$select=LogicalName&**$expand=Attributes($select=LogicalName)**&$filter=SchemaName eq 'Contact'

在 returned JSON 中,您将在

处找到属性
value[0].Attributes

这是一个数组。在这种情况下,我 $select=LogicalName 所以每个人都有 属性 LogicalName(以及 MetadataId,它是 属性 的 GUID)。

因此第一个 $select 用于实体的属性。 $expand 告诉 CRM 包含属性。内部 $select 告诉属性上的哪些字段 return 。 $filter 确保他们只 return 联系人的所有这些元数据。

所提供的答案描述了如何获取实体的元数据,技术上确实具有给定实体的所有定义。 但是,当使用 Web API 时,另一种方法是下载 Web API 的 CSDL 元数据文档($metadata)并查找联系人实体类型的定义。这是用于生成此文档的数据 contact EntityType 并提供 Web API.

使用的特定定义

你会看到结构上的差异。即元数据定义 LookupAttributeMetadata 属性,但 Web API 使用单值导航属性和只读 'lookup properties'.

有关了解 Web API $元数据的信息,请参阅 Web API types and operations > Entity types