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:
- Validating an Option Set value
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
//Dictionary to be defined globally: Dictionary<string, int> dOptionSetName = new Dictionary<string, int>(); private void LoadOptionSet(String optionSetName, ref IOrganizationService oServicProxy) { try { RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest { Name = optionSetName }; RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)oServiceProxy.Execute(retrieveOptionSetRequest); OptionSetMetadata retrievedOptionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata; OptionMetadata[] optionList = retrievedOptionSetMetadata.Options.ToArray(); //Add the options to our dictionary foreach (OptionMetadata oMD in optionList) { dOptionSetName.Add(oMD.Label.UserLocalizedLabel.Label.ToString(), (int)oMD.Value); } } catch (Exception ex) { //Exception code here: } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
OptionSetValue oLeadSourceValue = new OptionSetValue(); string sPassedOptionSetValue = "New Option Set Value"; if (dOptionSetName.TryGetValue(sPassedOptionSetValue, out int iOptSetValue)) { //Set the integer value to the value found in the dictionary: oLeadSourceValue.Value = iOptSetValue; } else { //Create a new Option Set Value: var oInsertOptionSetValue = new InsertOptionValueRequest { AttributeLogicalName = "leadsourcecode", EntityLogicalName = "lead", Label = new Label(sPassedOptionSetValue, 1033), }; int iNewOption = ((InsertOptionValueResponse) oServiceProxy.Execute(oInsertOptionSetValue)).NewOptionValue; oLeadSourceValue.Value = iNewOption; } oLead["leadsourcecode"] = oLeadSourceValue; |
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.
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.
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