在 Dynamics CRM 中使用 OData 检索所有 OptionSet 值

Retrieve all OptionSet values using OData in Dynamics CRM

我对 Dynamics CRM 还很陌生。我正在构建一个应在 Dynamics CRM 中更新实体的应用程序。我可以毫无问题地更新简单类型。现在的情况是,我已经在 Contact 实体中声明了一些自定义选项集。

是否有任何方法可以检索所有可能的 OptionSet 值(文本和值),以便我的应用程序可以查找适当的值并将其设置在它生成的有效负载中?

我在 WebAPIXRMServices/2011/OrganizationData.svc 中找不到任何端点。任何帮助都会非常棒。

您可以使用 Web API 或组织服务来检索 The metadata and data models in Microsoft Dynamics CRM。查看该文章的子文章以获取具体示例和详细信息。

Web API 示例 Querying EntityMetadata attributes.

The following query will return only the PicklistAttributeMetadata attributes and will include the LogicalName as well as expanding the OptionSet and GlobalOptionSet collection-valued navigation properties.

GET [Organization URI]/api/data/v8.1/EntityDefinitions(70816501-edb9-4740-a16c-6a5efbc05d84)/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet,GlobalOptionSet

使用以下代码为特定实体获取特定选项集: (用您的输入参数替换 EntityLogicalName 和 AttributeLogicalName)

GET [Organization URI]/api/data/v9.1/EntityDefinitions(LogicalName='EntityLogicalName')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet($select=Options),GlobalOptionSet($select=Options)&$filter=LogicalName eq 'AttributeLogicalName'

另一种选择是通过 StringMap 实体获取数据:

[Organization URI]/api/data/v9.1/stringmaps?fetchXml=<fetch><entity name='stringmap'><filter><condition attribute='objecttypecodename' operator='in'><value>account</value><value>opportunity</value></condition></filter></entity></fetch>

将提供如下所示的数据:

{
"@odata.etag": "W/\"406742363\"",
"value": "Open",
"attributename": "statecode",
"langid": 1033,
"objecttypecode": "opportunity",
"attributevalue": 0,
"stringmapid": "0fe09734-3914-e711-80ef-e0071b6a7121",
"organizationid": "f95718b2-5c63-46df-adc3-c3b546cf686a",
"displayorder": 1
},
{
"@odata.etag": "W/\"406742364\"",
"value": "Won",
"attributename": "statecode",
"langid": 1033,
"objecttypecode": "opportunity",
"attributevalue": 1,
"stringmapid": "10e09734-3914-e711-80ef-e0071b6a7121",
"organizationid": "f95718b2-5c63-46df-adc3-c3b546cf686a",
"displayorder": 2
},

更简单的查询:

[Organization URI]/api/data/v9.1/stringmaps?$filter=objecttypecode eq 'account' or objecttypecode eq 'opportunity'

如果您的选项是全局选项,这是获取所有选项的最简单方法:

/api/data/v9.1/GlobalOptionSetDefinitions(Name='new_category')