Get OData option set values and names

烈酒焚心 提交于 2019-12-23 02:34:49

问题


I am using odata api, now I have an attribute on an entity that is an option select like :

attribute name is : status values are: 1, 2, 3 names: done, progress, new

the thing is when I am using postman to fetch metadata and all I get for the fields 'status' its that its type integer.

Question how do I fetchj option names and values from metadata so I get values and names in response ?

Currently I get this:

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

But I want to get the value and names on response ?


回答1:


According to this article this is a multi-step process.

Please note that all the examples use HTTP GET.

First retrieve the entity's MetaData Id (for this example we're using the entity 'account'): https://myOrg.crm.dynamics.com/api/data/v8.2/EntityDefinitions?$select=LogicalName,MetadataId&$filter=LogicalName eq 'account'

Returns:

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

Then retrieve the attribute's MetaDataId (in this example we're using the option set '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')

Returns:

{
    "@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"
    }]
}

Then query with the entity's and attribute's MetadataIds to get the option set values: 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

Returns (truncated at 2 values):

{
    "@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
            }
        }
    }
}



回答2:


This could be vastly simplified, assuming all you want are the int values, and names for a particular option set attribute of an entity:

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)

The $select=LogicalName is just so it doesn't return all the other metadata for the attribute, and the $expand=OptionSet($select=Options) is for local option sets, and the GlobalOptionSet($select=Options) is for Global. If you know what type it is, you can skip it the other, but if you are putting this logic in a shared library (you are aren't you?) then adding both won't hurt:

{
   "@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"
   }
}


来源:https://stackoverflow.com/questions/51591182/get-odata-option-set-values-and-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!