Skip to main content
Post a question

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Get the label of an OptionSetValue

Like (0) ShareShare
ReportReport
Posted on 13 Jun 2018 12:15:53 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

  • yleclerc Profile Picture
    yleclerc 1,547 on 15 Apr 2021 at 14:24:31
    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
    k3000 65 on 13 Jun 2018 at 12:53:39
    RE: Get the label of an OptionSetValue

    Thank you! That worked:)

  • Verified answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on 13 Jun 2018 at 12:47:03
    RE: Get the label of an OptionSetValue

    Hi,

    Did you try

    string label=image.FormattedValues["new_kanheisbrukes"]

  • k3000 Profile Picture
    k3000 65 on 13 Jun 2018 at 12:40:56
    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
    gdas 50,087 on 13 Jun 2018 at 12:23:13
    RE: Get the label of an OptionSetValue

    Hi ,

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

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

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

    1. private string GetOptionsSetTextForValue(IOrganizationService service, string entityName, string attributeName, int selectedValue)
    2. {
    3.  
    4. RetrieveAttributeRequest retrieveAttributeRequest = new
    5. RetrieveAttributeRequest
    6. {
    7. EntityLogicalName = entityName,
    8. LogicalName = attributeName,
    9. RetrieveAsIfPublished = true
    10. };
    11. // Execute the request.
    12. RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
    13. // Access the retrieved attribute.
    14. Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)
    15. retrieveAttributeResponse.AttributeMetadata;// Get the current options list for the retrieved attribute.
    16. OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
    17. string selectedOptionLabel = null;
    18. foreach (OptionMetadata oMD in optionList)
    19. {
    20. if (oMD.Value == selectedValue)
    21. {
    22. selectedOptionLabel = oMD.Label.LocalizedLabels[0].Label.ToString();
    23. break;
    24. }
    25. }
    26. return selectedOptionLabel;
    27. }
  • Suggested answer
    Irena Benja Profile Picture
    Irena Benja 412 on 13 Jun 2018 at 12:21:34
    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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,234 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,994 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading complete