获取OData选项设置值和名称

时间:2018-07-30 09:44:37

标签: odata dynamics-crm microsoft-dynamics

我正在使用odata api,现在我在实体上有一个属性,可以选择:

属性名称是:status 值是:1, 2, 3 名称:done, progress, new

问题是当我使用邮递员获取元数据时,对于字段“状态”,我所获得的全部是其类型整数。

问题我如何从元数据中获取选项名称和值,以便在响应中获取值和名称?

当前,我得到了:

  <Property Name="status" Type="Edm.Int32">
     <Annotation Term="Org.OData.Core.V1.Description" String="" />
  </Property>

但是我想在响应中获取值和名称吗?

2 个答案:

答案 0 :(得分:2)

假设您只需要int值和实体特定选项集属性的名称,则可以大大简化此操作:

GET [Organization URI]/api/data/v8.2/EntityDefinitions(LogicalName='contact')/Attributes(LogicalName='status')/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet($select=Options),GlobalOptionSet($select=Options)

$select=LogicalName只是它不会返回该属性的所有其他元数据,而$expand=OptionSet($select=Options)是用于本地选项集的,而GlobalOptionSet($select=Options)是用于全局的。如果您知道它是什么类型,则可以跳过另一个类型,但是如果您将此逻辑放在共享库中(不是吗?),那么将两者都添加不会有什么害处:

{
   "@odata.context":"http://YourOrg.com/YourInstance/api/data/v8.2/$metadata#EntityDefinitions('new_entity')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata(LogicalName,OptionSet,GlobalOptionSet,OptionSet(Options),GlobalOptionSet(Options))/$entity",
   "LogicalName":"new_familyshortname",
   "MetadataId":"dc11c01f-b6bd-4664-82d0-3a521841c1f5",
   "OptionSet@odata.context":"http://YourOrg.com/YourInstance/api/data/v8.2/$metadata#EntityDefinitions('new_entity')/Attributes(dc11c01f-b6bd-4664-82d0-3a521841c1f5)/Microsoft.Dynamics.CRM.PicklistAttributeMetadata/OptionSet(Options)/$entity",
   "OptionSet":null,
   "GlobalOptionSet":{
      "@odata.type":"#Microsoft.Dynamics.CRM.OptionSetMetadata",
      "Options":[
         {
            "Value":117280000,
            "Label":{
               "LocalizedLabels":[
                  {
                     "Label":"English Value 1",
                     "LanguageCode":1033,
                     "IsManaged":true,
                     "MetadataId":"3cb6bbd5-796f-e111-8cf3-3cd92b023782",
                     "HasChanged":null
                  },
                  {
                     "Label":"French Value 1",
                     "LanguageCode":1036,
                     "IsManaged":false,
                     "MetadataId":"d88be67d-4a7d-e411-8890-0050569f1654",
                     "HasChanged":null
                  }
               ],
               "UserLocalizedLabel":{
                  "Label":"English Value 1",
                  "LanguageCode":1033,
                  "IsManaged":true,
                  "MetadataId":"3cb6bbd5-796f-e111-8cf3-3cd92b023782",
                  "HasChanged":null
               }
            },
            "Description":{
               "LocalizedLabels":[
                  {
                     "Label":"",
                     "LanguageCode":1033,
                     "IsManaged":true,
                     "MetadataId":"3db6bbd5-796f-e111-8cf3-3cd92b023782",
                     "HasChanged":null
                  }
               ],
               "UserLocalizedLabel":{
                  "Label":"",
                  "LanguageCode":1033,
                  "IsManaged":true,
                  "MetadataId":"3db6bbd5-796f-e111-8cf3-3cd92b023782",
                  "HasChanged":null
               }
            },
            "Color":null,
            "IsManaged":true,
            "MetadataId":null,
            "HasChanged":null
         }, 
         ... MORE ...
      ],
      "MetadataId":"dcbbe460-bedb-4985-9a17-2f3dbc637594"
   }
}

答案 1 :(得分:1)

根据this article,这是一个多步骤过程。

请注意,所有示例均使用HTTP GET。

首先获取实体的MetaData ID(在此示例中,我们使用的是实体“帐户”): https://myOrg.crm.dynamics.com/api/data/v8.2/EntityDefinitions?$select=LogicalName,MetadataId&$filter=LogicalName eq 'account'

返回:

{
    "@odata.context": "https://myOrg.crm.dynamics.com/api/data/v8.2/$metadata#EntityDefinitions(LogicalName,MetadataId)",
    "value": [{
        "LogicalName": "account",
        "MetadataId": "70816501-edb9-4740-a16c-6a5efbc05d84"
    }]
}

然后检索属性的MetaDataId(在此示例中,我们使用选项集“ customertypecode”): https://myOrg.crm.dynamics.com/api/data/v8.2/EntityDefinitions(70816501-edb9-4740-a16c-6a5efbc05d84)?$select=LogicalName&$expand=Attributes($select=LogicalName;$filter=LogicalName eq 'customertypecode')

返回:

{
    "@odata.context": "https://myOrg.crm.dynamics.com/api/data/v8.2/$metadata#EntityDefinitions(LogicalName,Attributes(LogicalName))/$entity",
    "LogicalName": "account",
    "MetadataId": "70816501-edb9-4740-a16c-6a5efbc05d84",
    "Attributes@odata.context": "https://myOrg.crm.dynamics.com/api/data/v8.2/$metadata#EntityDefinitions(70816501-edb9-4740-a16c-6a5efbc05d84)/Attributes(LogicalName)",
    "Attributes": [{
        "@odata.type": "#Microsoft.Dynamics.CRM.PicklistAttributeMetadata",
        "LogicalName": "customertypecode",
        "MetadataId": "4e33af09-ba43-4365-a747-c7e4f9992172"
    }]
}

然后使用实体和属性的MetadataIds查询以获取选项集值: https://myOrg.crm.dynamics.com/api/data/v8.2/EntityDefinitions(70816501-edb9-4740-a16c-6a5efbc05d84)/Attributes(4e33af09-ba43-4365-a747-c7e4f9992172)/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet

返回值(截断为2个值):

{
    "@odata.context": "https://myOrg.crm.dynamics.com/api/data/v8.2/$metadata#EntityDefinitions(70816501-edb9-4740-a16c-6a5efbc05d84)/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata(LogicalName,OptionSet)/$entity",
    "LogicalName": "customertypecode",
    "MetadataId": "4e33af09-ba43-4365-a747-c7e4f9992172",
    "OptionSet@odata.context": "https://myOrg.crm.dynamics.com/api/data/v8.2/$metadata#EntityDefinitions(70816501-edb9-4740-a16c-6a5efbc05d84)/Attributes(4e33af09-ba43-4365-a747-c7e4f9992172)/Microsoft.Dynamics.CRM.PicklistAttributeMetadata/OptionSet/$entity",
    "OptionSet": {
        "MetadataId": "3629e642-b895-41ab-8f1d-ea5bfa30e992",
        "HasChanged": null,
        "IsCustomOptionSet": false,
        "IsGlobal": false,
        "IsManaged": true,
        "Name": "account_customertypecode",
        "ExternalTypeName": null,
        "OptionSetType": "Picklist",
        "IntroducedVersion": "5.0.0.0",
        "Description": {
            "LocalizedLabels": [{
                "Label": "Type of the account.",
                "LanguageCode": 1033,
                "IsManaged": true,
                "MetadataId": "73f68e38-c78d-48a5-80cb-bee895baab2b",
                "HasChanged": null
            }],
            "UserLocalizedLabel": {
                "Label": "Type of the account.",
                "LanguageCode": 1033,
                "IsManaged": true,
                "MetadataId": "73f68e38-c78d-48a5-80cb-bee895baab2b",
                "HasChanged": null
            }
        },
        "DisplayName": {
            "LocalizedLabels": [{
                "Label": "Relationship Type",
                "LanguageCode": 1033,
                "IsManaged": true,
                "MetadataId": "e5d47366-fd09-41e6-96a1-cbfdd113b932",
                "HasChanged": null
            }],
            "UserLocalizedLabel": {
                "Label": "Relationship Type",
                "LanguageCode": 1033,
                "IsManaged": true,
                "MetadataId": "e5d47366-fd09-41e6-96a1-cbfdd113b932",
                "HasChanged": null
            }
        },
        "IsCustomizable": {
            "Value": true,
            "CanBeChanged": false,
            "ManagedPropertyLogicalName": "iscustomizable"
        },
        "Options": [{
            "Value": 1,
            "Color": null,
            "IsManaged": true,
            "ExternalValue": null,
            "MetadataId": null,
            "HasChanged": null,
            "Label": {
                "LocalizedLabels": [{
                    "Label": "Competitor",
                    "LanguageCode": 1033,
                    "IsManaged": true,
                    "MetadataId": "6c54c2fa-2241-db11-898a-0007e9e17ebd",
                    "HasChanged": null
                }],
                "UserLocalizedLabel": {
                    "Label": "Competitor",
                    "LanguageCode": 1033,
                    "IsManaged": true,
                    "MetadataId": "6c54c2fa-2241-db11-898a-0007e9e17ebd",
                    "HasChanged": null
                }
            },
            "Description": {
                "LocalizedLabels": [],
                "UserLocalizedLabel": null
            }
        },
        {
            "Value": 2,
            "Color": null,
            "IsManaged": true,
            "ExternalValue": null,
            "MetadataId": null,
            "HasChanged": null,
            "Label": {
                "LocalizedLabels": [{
                    "Label": "Consultant",
                    "LanguageCode": 1033,
                    "IsManaged": true,
                    "MetadataId": "6e54c2fa-2241-db11-898a-0007e9e17ebd",
                    "HasChanged": null
                }],
                "UserLocalizedLabel": {
                    "Label": "Consultant",
                    "LanguageCode": 1033,
                    "IsManaged": true,
                    "MetadataId": "6e54c2fa-2241-db11-898a-0007e9e17ebd",
                    "HasChanged": null
                }
            },
            "Description": {
                "LocalizedLabels": [],
                "UserLocalizedLabel": null
            }
        }
    }
}