0
votes

I'm trying to create a "Global OptionSet"-attribute (sd_MyAttribute) for an existing entity (entity ID = 70816501-edb9-4740-a16c-6a5efbc05d84) via Dynamics CRM WebAPI. The JSON I send is this using method "POST":

{
  "@odata.type": "Microsoft.Dynamics.CRM.PicklistAttributeMetadata",
  "OptionSet": {
    "@odata.type": "Microsoft.Dynamics.CRM.OptionSetMetadata",
    "IsGlobal": true,
    "Name": "sd_MyPickList",
    "OptionSetType": "Picklist",
    "MetadataId": "a50cfc0a-e206-ea11-a811-000d3ab82e70"
  },
  "AttributeType": "Picklist",
  "SchemaName": "sd_MyAttribute",
  "Description": {
    "@odata.type": "Microsoft.Dynamics.CRM.Label",
    "LocalizedLabels": [
      {
        "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
        "Label": "This is the attribute I want to create.",
        "LanguageCode": 1033
      }
    ]
  },
  "DisplayName": {
    "@odata.type": "Microsoft.Dynamics.CRM.Label",
    "LocalizedLabels": [
      {
        "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
        "Label": "This is the attribute I want to create.",
        "LanguageCode": 1033
      }
    ]
  },
  "RequiredLevel": {
    "Value": "None",
    "CanBeChanged": true
  }
}

I expected to get a status 204 response, indicating that a new Picklist attribute on the entity using the sd_MyPickList option set has been created.

Unfortunately, the response is:

{
  "error": {
    "code": "0x80048403",
    "message": "Only Local option set can be created through the attribute create. IsGlobal flag must be set to 'false'.",
    "innererror": {
      "message": "Only Local option set can be created through the attribute create. IsGlobal flag must be set to 'false'.",
      "type": "Microsoft.Crm.CrmException",
      "stacktrace": "   ...)"
    }
  }
}

There is already an issue in the github project (see https://github.com/MicrosoftDocs/dynamics-365-customer-engagement/issues/601), but I wonder whether there is a way around this problem - what json do I need to send to create an attribute adressing a global option set? Is there someone who has successfully created such an entity attribute via web-api?

There is a usecase, I don't have the ability to use an existing library for that and importing a solution is not an option in my case.

Would be perfect if someone can provide a simple json that can be send e.g. using the Contact entity and any global optionset.

1

1 Answers

3
votes

Finally, I found a way to accomplish what I need. To specify the global option set I need to use the "@odata.bind" action in the JSON data. For an attribute "sd_MyAttribute" that uses the global OptionSet with the MetaDataId "62654906-7A0b-ea11-a817-000d3ab826fd", I need to do POST:

{
  "@odata.type": "Microsoft.Dynamics.CRM.PicklistAttributeMetadata",
  "[email protected]": "/GlobalOptionSetDefinitions(62654906-7A0b-ea11-a817-000d3ab826fd)",
  "AttributeType": "Picklist",
  "SchemaName": "sd_MyAttribute",
  "Description": { ... },
  "DisplayName": { ... },
  "RequiredLevel": { ... }
}

If the entity does have the MetaDataId "70916b01-edb2-4840-a16b-6a2efbc75d84", the URI for the POST would be "/api/data/v9.0/EntityDefinitions(70916b01-edb2-4840-a16b-6a2efbc75d84)/Attributes" (logical- or schema-names are not supported).

Hope my question and answer does help someone who gets the same error message.