devklick
devklick

Reputation: 2629

C# - Set CRM OptionSetValue using only the string name of the option

Is it possible to set a new OptionSetValue with a Dynamics CRM using only the name / label of the option?

For example, I have the following OptionSet:

1 : Male
2 : Female

If I don't have the int value, is it possible to use the string label instead? Such as...

my_optionSet = new OptionSetValue(Male)

Upvotes: 2

Views: 11336

Answers (3)

D-Shih
D-Shih

Reputation: 46219

In your case I would use a Dictionary<string, int> be a mapper table then you can pass your string value then set then int parameter in OptionSetValue class.

Dictionary<string, int> mapperT = new Dictionary<string, int>();
mapperT.Add("Male", 1);
mapperT.Add("Female", 2);
my_optionSet = new OptionSetValue(mapperT["Male"]);

Upvotes: 3

Nick
Nick

Reputation: 755

In addition to Arun answer. You can generate the Early-Bound classes and get the options set generated for you. There is a tool in the XrmToolbox for this:

enter image description here

Upvotes: 2

We use enum to achieve it.

public enum Gender
{
    Male = 864630000,
    Female = 864630001,    
};

my_optionSet = new OptionSetValue((int)Gender.Male);

These are going to be pre-defined key:value pairs that never changes between environments. When you think about avoiding code deployments & refined to schema solution only deployments whenever new options getting added in that attribute, you can invest on Retrieve OptionSet Metadata.

Upvotes: 5

Related Questions