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

Loading