Softstuff Consulting
My occasional musings of a technical nature
  • Send us a message
  • About Softstuff Consulting
  • Privacy Policy
  • Main Website
C# , Dynamics365

Getting OptionSet values out of CRM2015 with C#

by Ian Blair February 23, 2019 No Comments

Every so often when you are reading a record in code the integer values from the OptionSet fields arent enough because you want to display the whole of the data contained in the record and make it fit for user consumption.

Unfortunately the only way is to pull out the OptionSet values from the fields separately and then do the comparison in code to find the correct text value, but making your own interfaces multilingual or populating your own lists for record creation then becomes quite straightforward.

A function like this will pull out a List of KeyValuePair containing the numeric value of the OptionSetValue and the text value local to the user who is running the code. It is quite easy to extend this to only pull out the text values for a specific user locale.

public static List<KeyValuePair<int, string>> getOptionSetText(IOrganizationService service, string entityName, string attributeName)
{
    List<KeyValuePair<int, string>> values = new List<KeyValuePair<int, string>>();
    RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest();
    retrieveAttributeRequest.EntityLogicalName = entityName;
    retrieveAttributeRequest.LogicalName = attributeName;
    retrieveAttributeRequest.RetrieveAsIfPublished = false;
    RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
    PicklistAttributeMetadata picklistAttributeMetadata = (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
    OptionSetMetadata optionsetMetadata = picklistAttributeMetadata.OptionSet;
 
    foreach (OptionMetadata optionMetadata in optionsetMetadata.Options)
    {
        if (optionMetadata.Value.HasValue)
        {
            values.Add(new KeyValuePair<int, string>(optionMetadata.Value.Value, optionMetadata.Label.UserLocalizedLabel.Label));
        }
    }
    return values;
}

To retrieve localised OptionSetValue text instead of using optionMetaData.Label.UnserLocalizedLabel.Label you could substitute it for something like

foreach (LocalizedLabel l in optionMetadata.Label.LocalizedLabels)
{
    string labelText = l.Label;
    int lang = l.LanguageCode;
}

This will loop around all the localised values to extract the text and the locale id code.

code

  • Previous Preventing infinite loops in CRM2013/2015/2016 plugins2 years ago
  • Next Reading marketing list members from CRM2 years ago

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • A Dynamics365 plugin thought experiment
  • Registering a Dynamics365 plugin and I get an error
  • Going back in time with Dynamics365
  • Make using Windows for people with eyesight issues easier
  • Lets not forget VB script

Categories

  • Bootstrap
  • C#
  • CSS
  • Dot Net Core
  • Dynamics365
  • JavaScript
  • T-SQL
  • Thoughts
  • VBScript
  • Visual Studio
  • Windows
  • Xamarin

Recent Comments

  • TC Sharpe on Throw exceptions in Dynamics365 workflow plugins, or don’t
  • BigOwl on Throw exceptions in Dynamics365 workflow plugins, or don’t
  • CRMGod on Access a Dynamics365 embedded webresource
  • Mike M on Access a Dynamics365 embedded webresource

Archives

  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • June 2019
  • May 2019
  • February 2019
2021 Softstuff Consulting. Donna Theme powered by WordPress
  • Twitter
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.I am happy