9

I am quite new to Dynamics CRM. I am building an app which should update entity in Dynamics CRM. I can update simple types without any issues. Now the situation is, I have declared some custom Option Sets in Contact entity.

Is there any way to retrieve all the possible OptionSet values (text and value) so that my app can look for appropriate value and set it in the payload it is generating?

I can not find any endpoint in WebAPI as well as XRMServices/2011/OrganizationData.svc. Any help would be really awesome.

Rahul Patil
  • 5,656
  • 6
  • 37
  • 65

5 Answers5

20

You can use either the Web API or Organisation Service to retrieve The metadata and data models in Microsoft Dynamics CRM. Check out the sub articles of that one for specific examples and details.

Web API example 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
James Wood
  • 17,286
  • 4
  • 46
  • 89
  • This is for a Two Options (AttributeType === 'Boolean'): Microsoft.Dynamics.CRM.BooleanAttributeMetadata – Niels Steenbeek Sep 29 '17 at 10:55
  • Hello, I am also new to CRM and have a similar use case. 1. The above Query gives me Global Optoinset data but not for Optionsets defined at Entity level 2. Even for Global Optionsets, I did not get all the data. 3. What does the GUID - 70816501-edb9-4740-a16c-6a5efbc05d84 specify, is it a Universal value? If not, Where can I get the corresponding value. Any link detailing this further is highly appreciated. – SKocheta Mar 05 '18 at 11:20
  • 1
    **Update** : Was able to get this via: [Organization URI]/api/data/v8.2/EntityDefinitions(LogicalName='[Entity Name]')/Attributes(LogicalName='[Your Field Name Which uses OptionSet]')/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet($select=Options),GlobalOptionSet($select=Options) – SKocheta Mar 05 '18 at 11:45
2

Another option would be to get the data via the StringMap entity:

[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>

Will provide data that looks like this:

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

Simpler query:

[Organization URI]/api/data/v9.1/stringmaps?$filter=objecttypecode eq 'account' or objecttypecode eq 'opportunity'
Raj Rao
  • 8,872
  • 12
  • 69
  • 83
1

Use below code to get specific option set for specific entity: (replace EntityLogicalName and AttributeLogicalName with your input params)

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'
1

If your options are global, this is the easiest way to get all options:

/api/data/v9.1/GlobalOptionSetDefinitions(Name='new_category')
CularBytes
  • 9,924
  • 8
  • 76
  • 101
0

First, you need the attribute name and type, which you can find here:

/api/data/v9.1/EntityDefinitions(LogicalName='myEntity')?$select=LogicalName&$expand=Attributes($select=LogicalName)

Replace myEntity above with your entity name. The resulting page lists all the attributes that make up your entity. Locate the desired option set attribute and note its logical name and type.

Armed with that information, go here:

/api/data/v9.1/EntityDefinitions(LogicalName='myEntity')/Attributes(LogicalName='myAttribute')/Microsoft.Dynamics.CRM.MultiSelectPicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet($select=Options),GlobalOptionSet($select=Options)

Replace myEntity and myAttribute with your entity name and attribute name, respectively. If your attribute type is not MultiSelectPicklistAttributeMetadata, replace that with the correct type that was found on the previous page. This returns a list of all the possible values for the option set (both text and numeric values).

Tawab Wakil
  • 1,737
  • 18
  • 33