Dynamics 365 Option Sets: Validate and Create them in code (C#)


A common question when writing a C# interface for Dynamics 365 is to how to deal with Option Sets. In this post I’m going to share with you a couple of  useful functions that I use to deal with the following 2 requirements:

  1. Validating an Option Set value
  2. Creating a new Option Set value if it does not already exist

To validate an Option Set value I load all current Option Set values into a global .Net Dictionary via the Dynamics 365 SDK. The .Net Dictionary will hold the text and integer value of the Option Set.

Below is an example of the c# code I use to do this. The function LoadOptionSet takes a couple of parameters:

  • optionSetName = The schema name of the option set field we need to load. E.g. leadsourcecode for the Lead Source field on the Lead entity
  • oServiceProxy = a reference to the already defined Organization Service

Once the Dictionary is loaded we can then use it to search for the text value and if found we can use the integer to set the value within our code using OptionSetValue.

If it’s not found you can either throw an exception or depending on the circumstance we can create a new Option Set value within the code to save user intervention.

The C# code snippet below shows how to search the .Net Dictionary for the passed option set value (variable sPassedOptionSetValue), if it’s found the OptionSetValue variable is set to the integer value. If it’s not found I create a new Option Set value in Dynamics within the else statement.

I’m using the Lead Source field on the Lead entity in this example:

Note: when setting the new Option Set “Label” property, 1033 is the language code for English. You will need to tweak this for other languages. I also didn’t include the Value property here so that the system would automatically assign the next integer value, but you can include it if you want more control over the ID that is assigned to the new value.

If you have any issues or have any questions feel free to leave a comment.

Useful Links

2 thoughts on “Dynamics 365 Option Sets: Validate and Create them in code (C#)

  1. Hi Glen,
    Thank you so much for the article. I used your code to get Data from SharePoint on-premise to Dynamics 365 Online using Console Application.
    The code working but very often (after ca. 10 – 15 data ) I got Error
    “System.ServiceModel.FaultException`1: “A validation error occurred. The value 778210052 of ‘ap_territoryarea’ on record of type ‘ap_costcentre’ is outside the valid range. Accepted Values: ” and I must start the app again. Also Console Application running very slowly.
    Any advice will be great. Thanks in advance.

    1. Hi Necdet,

      It sounds like the option set value being placed in the field ‘ap_territoryarea’ is not a valid option in the list. Take a look at the problematic value and compare it against the option set. Also check that the most recent changes to the field have been published.

      Cheers,
      Glenn

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts