web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Get the label of an OptionSetValue

(0) ShareShare
ReportReport
Posted on by 65

I have this code line in a plugin which pulls the value from the optionset field:

int elevatorOption = image.GetAttributeValue<OptionSetValue>("new_kanheisbrukes").Value;

This works fine, however it only gives me the integer value, i.e. 100 000 001. How can I get the label value, which is a string?

*This post is locked for comments

I have the same question (0)
  • yleclerc Profile Picture
    1,549 on at
    RE: Get the label of an OptionSetValue

    Hi there!

    Thanks for this solution. It works for us

    string label=image.FormattedValues["new_kanheisbrukes"]

    The only thing is we have a bilingual portal. And values only show in the main language. How can we display the values in relation with the portal language?

    P.S. Yes, our labels in CRM are already translated.

  • k3000 Profile Picture
    65 on at
    RE: Get the label of an OptionSetValue

    Thank you! That worked:)

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at
    RE: Get the label of an OptionSetValue

    Hi,

    Did you try

    string label=image.FormattedValues["new_kanheisbrukes"]

  • k3000 Profile Picture
    65 on at
    RE: Get the label of an OptionSetValue

    Any special references I need? I am using these two at the moment

    using System;

    using Microsoft.Xrm.Sdk;

    I am getting a few error messages on missing types or namespaces...

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Get the label of an OptionSetValue

    Hi ,

    You can use FormattedValues to retrieve the label try with below code .

    string optionSetText= entity.FormattedValues["FieldName"];

    In addition also try to call below method and pass required parameter , you will get the label using metadata.

            private string GetOptionsSetTextForValue(IOrganizationService service, string entityName, string attributeName, int selectedValue)
            {
    
                RetrieveAttributeRequest retrieveAttributeRequest = new
                RetrieveAttributeRequest
                {
                    EntityLogicalName = entityName,
                    LogicalName = attributeName,
                    RetrieveAsIfPublished = true
                };
                // Execute the request.
                RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
                // Access the retrieved attribute.
                Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)
                retrieveAttributeResponse.AttributeMetadata;// Get the current options list for the retrieved attribute.
                OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
                string selectedOptionLabel = null;
                foreach (OptionMetadata oMD in optionList)
                {
                    if (oMD.Value == selectedValue)
                    {
                        selectedOptionLabel = oMD.Label.LocalizedLabels[0].Label.ToString();
                        break;
                    }
                }
                return selectedOptionLabel;
            }
  • Suggested answer
    Irena Benja Profile Picture
    414 on at
    RE: Get the label of an OptionSetValue

    Hello,

    You should work with FormattedValues property, see here:

    nishantrana.me/.../get-optionset-label-using-formattedvalues-property-of-entity-in-plugin-in-crm

    Regards,

    Irena.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans