Get the text from a CRM option list through it's value

When you retrieve a record from CRM and one of the fields is a so called option set you might have noticed that only the numeric value is available.

With lookup fields (entity references) you can get the Id and Name directly but with option sets that doesn't work. I've seen lots of ways to retrieve the text via a value but this one I wanted to share with you because it is simple and straight forward.

First I create two methods in my Utils class:

public class Utils
{
    private static OptionMetadataCollection GetOptionSet(string entityName, string fieldName, IOrganizationService service, bool excludeNull)
    {
        var attReq = new RetrieveAttributeRequest();
        attReq.EntityLogicalName = entityName;
        attReq.LogicalName = fieldName;
        attReq.RetrieveAsIfPublished = true;

        var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
        var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;

        var optionList = attMetadata.OptionSet.Options;
        return optionList;
    }

    public static Dictionary<int, string> GetOptionSet(string entityName, string fieldName, IOrganizationService service)
    {
        var optionSet = GetOptionSet(entityName, fieldName, service, true);
        var optionList = optionSet.Where(o => o.Value != null)
        .Select(o => new { key = (int)o.Value, text = o.Label.UserLocalizedLabel.Label }).ToDictionary(k => k.key, t => t.text);

        if (!optionList.Any()) optionList.Add(0, null);
        return optionList;
    }
}

As you can see GetOptionSet("new_entityname", "new_optionlist", CrmService) retrieves all the values from a certain option list and puts them in a dictionary. In the second line you can see how to retrieve the text from the dictionary via the value that was available.

var optionList = Utils.GetOptionSet("new_entityname", "new_optionlist", CrmService); 
var optionText = optionList[OptionValue];

I think this is a nice and simple approach to this problem and works for both global and individual option sets.

Comments (13) -

  • Mateusz Wass

    10/23/2016 12:29:33 PM |

    Good work. Thank you!

  • essay writing company

    1/4/2017 7:16:32 AM |

    My husband is a programmer and he will help  me to understand the things that you talk here about.

  • essaywritingservice-s.com/

    1/18/2017 11:42:43 AM |

    Thank you for this valuable and beneficial information for me. It is very helpful to me in writing my project on computer.

  • http://penessays.com/guarantees

    1/18/2017 11:44:06 AM |

    I will ask for help from my brother. He will help me to understand this article because he programmer.

  • professional resume service

    1/28/2017 3:41:14 PM |

    Looks cool. i think that it will be more clear if you'll add more tips on the topic. I'll wait for them. Thank you.

  • paper writers

    4/3/2017 7:47:03 AM |

    It's not as easy as counting to three, but I like the way you interpret the following specifications. May God bless you! Keep up to amaze us!

  • check my essay - essay check

    5/17/2017 8:04:52 AM |

    An excellent system with a good option. It seems to me that this function is not needed there

  • TULC4wTZw5V

    8/9/2017 6:25:49 PM |

    334466 197585Black Ops Zombies� [...]some individuals still have not played this game. It�s hard to imagine or believe, but yes, some individuals are missing out on all of the fun.[...]� 47947

  • stationery cupboards pretoria

    8/10/2017 10:09:34 PM |

    818053 297350Does your weblog have a contact page? I�m having a tough time locating it but, I�d like to send you an e-mail. I�ve got some suggestions for your weblog you might be interested in hearing. Either way, great web site and I appear forward to seeing it expand over time. 188015

  • 954469 899434I�m not certain exactly why but this web internet site is loading extremely slow for me. Is anyone else having this concern or is it a dilemma on my finish? I�ll check back later and see if the dilemma still exists. 844403

  • Bola Nation

    8/11/2017 10:30:37 PM |

    89123 313603hi and thanks for the actual blog post ive recently been searching regarding this specific advice on-line for sum hours these days as a result thanks 877018

  • Oklahoma Background Check

    8/14/2017 11:03:06 AM |

    206916 864071In fact your creative  writing skills has encouraged me to get my own website now. Actually the blogging is  spreading its wings quickly. Your write up is a good example of it. 322766

  • hop over to these guys

    8/15/2017 8:21:42 PM |

    502104 53080Attractive portion of content. I simply stumbled upon your weblog and in accession capital to assert that I get in fact loved account your weblog posts. Anyway I will likely be subscribing to your augment and even I success you get admission to constantly rapidly. 139364

  • מיזוג אוויר

    8/17/2017 2:18:37 AM |

    783129 635679I really thankful to locate this internet site on bing, just what I was seeking for : D too bookmarked . 239879

  • 슈어맨

    8/20/2017 2:30:06 PM |

    523596 390404Can I just now say that of a relief to locate somebody who truly knows what theyre speaking about online. You in fact know how to bring a difficulty to light and function out it crucial. The diet require to see this and appreciate this side on the story. I cant believe youre no a lot more popular since you surely possess the gift. 850419

  • Betsson online Casino

    8/20/2017 10:15:07 PM |

    681914 52889This is some great details. I expect additional facts like this was distributed across the web today. 221475

  • 235690 185056This internet internet site may be a walk-through its the data you wanted in regards to this and didn�t know who must. Glimpse here, and you�ll completely discover it. 230480

  • overnight xanax

    8/28/2017 5:26:46 AM |

    102112 580010Hello!   I basically would like to give a huge thumbs up for the wonderful information you�ve here on this post. I might be coming back to your weblog for far more soon. 784730

  • pemandangan

    8/28/2017 10:21:27 PM |

    968768 352497I extremely    glad  to discover this  website on bing, just what I was seeking  for : D  as properly  saved to favorites . 278265

  • chimney repair

    8/30/2017 2:19:35 PM |

    719447 89093You designed some decent points there. I looked over the internet for your concern and discovered most people will go along with together along with your internet site. 894154

  • tiffany schmuck

    9/2/2017 3:56:39 PM |

    62928 256997You produced some decent factors there. I looked on the internet for the issue and discovered most individuals will go along with with your website. 361704

  • Heizfolie

    9/3/2017 8:47:49 PM |

    304359 702225obviously like your web-site however you have to test the spelling on quite a few of your posts. 159388

  • Fairchild Group Reviews

    9/5/2017 4:04:06 AM |

    781852 555120hello admin, your website page�s pattern is simple and clean and i like it. Your articles are incredible. Remember to maintain up the very good function. Greets.. 461410

  • Laser Hair Removal NYC

    9/6/2017 11:02:48 PM |

    105153 645130Hey! Do you know if they make any plugins to help with SEO? I�m trying to get my blog to rank for some targeted keywords but I�m not seeing extremely excellent results. In the event you know of any please share. Thanks! 257765

  • https://www.tojaenge.com

    9/10/2017 10:12:13 PM |

    265750 131891When I originally commented I clicked the -Notify me when new surveys are added- checkbox and from now on whenever a comment is added I purchase four emails sticking with exactly the same comment. Perhaps there is by any means you could get rid of me from that service? Thanks! 883020

  • great site

    9/11/2017 4:16:11 PM |

    179458 343226You seem to be extremely professional in the way you write.::�~  108045

  • 먹튀썰전

    9/14/2017 6:50:40 AM |

    541536 163133Excellently written article, doubts all bloggers offered the identical content since you, the internet has to be far better location. Please stay the best! 113482

  • Sistem Klinik

    9/15/2017 3:10:57 AM |

    141137 773439I visited  plenty of internet site but I  conceive this one  contains something special   in it in it 552583

  • Bola tangkas online

    9/17/2017 7:51:05 PM |

    279252 109769I would like to see more posts like this!.. Fantastic blog btw! reis Subscribed.. 592590

  • agen judi bola

    9/17/2017 8:06:18 PM |

    678222 406551extremely excellent good�this post deserves almost absolutely nothing �hahaha merely joking: S �nice write-up: P 658238

  • cosmetic dental

    9/19/2017 10:20:24 PM |

    68001 390535This style is incredible! You undoubtedly know how to maintain a reader amused. Between your wit and your videos, I was almost moved to start my own weblog (effectively, almost�HaHa!) Fantastic job. I really loved what you had to say, and a lot more than that, how you presented it. Too cool! 908219

  • Bdsm

    9/25/2017 4:36:42 AM |

    850778 309876I  believe  this  website has  some actually    fantastic   data for every person : D. 598673

  • go to the website

    9/25/2017 6:42:35 AM |

    674014 564063Good read. I just passed this onto a buddy who was performing some research on that. He just bought me lunch since I identified it for him!  Thus let me rephrase: Thanx for lunch! 771666

  • click here

    9/26/2017 10:21:24 PM |

    419704 591792As I site owner I believe the content material here is extremely superb, thanks for your efforts. 733244

  • seo company

    9/27/2017 8:40:09 AM |

    863717 971735Wonderful internet site you got here! Yoo man wonderful reads, post some far more! Im gon come back so greater have updated 928419

  • Bdsm slave positions

    9/28/2017 4:01:39 AM |

    882061 229576Deference to op , some  superb   entropy. 726583

  • find here

    9/28/2017 7:24:09 AM |

    557419 463790I believe this really is finest for you: Soccer, Football, Highlight, Live Streaming 625258

  • Festivales

    9/28/2017 4:09:57 PM |

    165477 904392Hey there! Very good stuff, do keep us posted when you finally post something like that! 701688

  • viral soccer game

    9/28/2017 4:10:27 PM |

    921478 881605Interesting point of view. I�m curious to believe what type of impact this would have globally? Sometimes people get slightly upset with global expansion. I�ll be about soon to look at your response. 79753

  • mondo sonoro

    9/28/2017 4:11:50 PM |

    819373 639342Hey there, Can I copy this post image and implement it on my private internet log? 888730

  • Skrota bil Kungsbacka

    10/29/2017 7:24:57 PM |

    När du skall skrota bilen i Kungsbacka kan vi hjälpa dig med bärgningen till skroten.

Loading