Announcements
Hi,
I've an option set field in my custom entity.
I'm able to retrieve text filed values, but when I tried to retrieve option set value I'm receiving the following error.
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary
Though I've declared the column logical name in the Columnset.
How to retrieve the option set value from my custom entity?
*This post is locked for comments
Thanks Vikas,
That works!!!!!
Hi,
Just try to check field is present or not in the collection of properties:
foreach (Entity e1 in results1.Entities)
{
if (e1.Attributes.ContainsKey("new_optionsetvalues"))
{
businessdivisionText = ((string)(e1.Attributes["new_optionsetvalues"])).Value;
}
}
And check this one:
If you are trying to get the value the use
businessdivisionText = e1.GetAttributeValue<OptionSetValue>("new_optionsetvalues").Value.ToString();
If trying to get the text, use below:
businessdivisionText = e1.FormattedValues["new_optionsetvalues"].ToString();
Please make sure you have a value selected in the dropdown. Otherwise you will get the same error. To avoid that error you can apply the check before getting the value
if(e1.Containts("new_optionsetvalues"))
{
businessdivisionText = e1.GetAttributeValue<OptionSetValue>("new_optionsetvalues").Value.ToString();
}
Hi Vikas,
Yes, I've verified all are in small characters.
Thanks
Hi,
My code is like as follows:
QueryExpression query1 = new QueryExpression { EntityName = "MyEntity", ColumnSet = new ColumnSet("new_branch", "new_optionsetvalues") };
EntityCollection results1 = Service.RetrieveMultiple(query1);
string businessdivisionText = null;
foreach(Entity e1 in results1.Entities)
{
businessdivisionText = (string)e1["new_optionsetvalues"];
}
Retrieving the value of the optionset should work if the name of the field is correct. If you are using late bound in the code, can yo verify that you are using the field name in small characters only? for ex. new_fieldname and not new_FieldName.
Hi,
Could you post your code with how toy retrieve the entity and access the attribute?
Thanks,
Aiden
André Arnaud de Cal...
294,120
Super User 2025 Season 1
Martin Dráb
232,871
Most Valuable Professional
nmaenpaa
101,158
Moderator