Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to retrieve option set value?

(0) ShareShare
ReportReport
Posted on by 445

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

  • Elangamban Profile Picture
    445 on at
    RE: How to retrieve option set value?

    Thanks Vikas,

    That works!!!!!

  • Suggested answer
    Jharana Baliyar Singh Profile Picture
    2,665 on at
    RE: How to retrieve option set value?

    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:

    stackoverflow.com/.../retrieve-the-value-selected-in-optionset-field-and-display-ita-value-in-a-text-f

  • Verified answer
    Community Member Profile Picture
    on at
    RE: How to retrieve option set value?

    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();

    }

  • Elangamban Profile Picture
    445 on at
    RE: How to retrieve option set value?

    Hi Vikas,

    Yes, I've verified all are in small characters.

    Thanks

  • Elangamban Profile Picture
    445 on at
    RE: How to retrieve option set value?

    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"];

    }

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: How to retrieve option set value?

    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.

  • Aiden Kaskela Profile Picture
    19,694 on at
    RE: How to retrieve option set value?

    Hi,

    Could you post your code with how toy retrieve the entity and access the attribute?

    Thanks,

     Aiden

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,120 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,871 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans