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

  • 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

Loading