Introduction:
With the release of Dynamics 365 v9.0 Microsoft introduced a new data type i.e. Multi Select Option Set with the help of this we can select more than one option set in Dynamics 365.
Multi-Select Option Set has a list of options and we can select more than one option from the list on selecting checkboxes. i.e we can search option set from the list of option.
In this blog we will discuss how to Get, Set, Add and Remove options in Multi-Select Option Set.
Recently, we came across below scenario,
Where, User wants to update Multi Select Option Set from another option set programmatically and also Ad & Remove options from Multi Select Option Set
Retrieve Multi-Select Option Set and copy into another option set programmatically:
Entity entity = _service.Retrieve("account", new Guid(old RecordId), new ColumnSet("new_multioption")); Entity accountEntity = new Entity ("account", new Guid(New RecordId)); accountEntity["new_multioption"] = entity.GetAttributeValue<OptionSetValueCollection>("new_multioption");
Set Multi Select Option Set programmatically:
For Single value
OptionSetValueCollection collectionOptionSetValues = new OptionSetValueCollection(); OptionSetValue optionSet = new OptionSetValue(optionsetValue); collectionOptionSetValues.Add(optionSet); entityAccount["new_multioption"] = collectionOptionSetValues; _service.Update(entityAccount);
For multiple values
string[] arr = { collection of option set value }; foreach(var item in arr) { collectionOptionSetValues.Add(new OptionSetValue(Convert.ToInt32(item))); } entityAccount["new_multioption"] = collectionOptionSetValues; _service.Update(entityAccount);
Remove Multi-Select Option Set programmatically:
Remove single value
formContext.getControl(multioptionset).removeOption(optionsetvalue);
Remove multiple values
string[] arr = { collection of option set value }; foreach(var item in arr) { formContext.getControl(multioptionset).removeOption(item); }
Add new options in Multi Select Option Set programmatically:
Add Single option value
formContext.getControl(multioptionset).addOption(new Xrm.OptionSetItem(value, text), position);
Add Multiple option value
var position = 0; var optArr = [Collection of option set ]; for (var i = optArr.length - 1; i >= 0; i--) { formContext.getControl(multioptionset).addOption(new Xrm.OptionSetItem(optArr [i].value, optArr [i].text), position++); }
Conclusion:
Using the code above user can Get, Set, Add and Remove Multi Select Option Set programmatically in Dynamics 365 v9.0.