Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to retrieve OptionSet using C#?

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hello!

I have a option set field that have two options A, B. values are 0 and 1 respectively.

Now I have retrieved a record containing the above option set field, with A in it.

How can I get that A from the option set in the record I retrieved?

I used the code below to get text fields

EntityCollection contacts = RetrieveContacts();

  foreach (Entity contact in contacts.Entities)

  {

     Guid contactId = contact.Id;

     string name = contact.Contains("name") ? contact["name"].toString() : "";

  }

 

Thank you!

*This post is locked for comments

  • Sid07 Profile Picture
    Sid07 285 on at
    RE: How to retrieve OptionSet using C#?

    Hi,

    Also in addition u can refer the below blog for more details:

    www.inogic.com/.../read-formatted-values-using-linq-queries-in-dynamics-365

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to retrieve OptionSet using C#?

    Thank you!

  • Verified answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: How to retrieve OptionSet using C#?

    For getting the option set value:

    int value = ((OptionSetValue)entity["yourattributename"]).Value;

    For getting the text:

    String text = entity.FormattedValues["yourattributename"].ToString();

    In the above code entity is the Entity object from which the optionset value/text to be retrieved. Please replace the attribute name with your case.

    mark my answer as verified if helpful

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans