Click here to Skip to main content
12,242,156 members (41,769 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

27.3K views
6 bookmarked
Posted

Get the OptionsetValue and OptionsetText for Dynamics CRM 2011

, 26 Feb 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Get the OptionsetValue and OptionsetText for Dynamics CRM 2011.

Introduction

In CRM 2011, when you try to get the value of an OptionSet, you will always get an integer value instead of the label value and also while setting the optionset, an integer value is required.

Using the code

Below is the helper code to fetch OptionSet text or value depending on the requirement.

static CRMHelper
{
   public static int getOptionSetValue(string entityName, string attributeName, string  optionsetText)
    {
       int optionSetValue = 0;
       RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest();
       retrieveAttributeRequest.EntityLogicalName = entityName;
       retrieveAttributeRequest.LogicalName = attributeName;
       retrieveAttributeRequest.RetrieveAsIfPublished = true;

       RetrieveAttributeResponse retrieveAttributeResponse = 
         (RetrieveAttributeResponse)OrganizationService.Execute(retrieveAttributeRequest);
       PicklistAttributeMetadata picklistAttributeMetadata = 
         (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;

       OptionSetMetadata optionsetMetadata = picklistAttributeMetadata.OptionSet;

       foreach (OptionMetadata optionMetadata in optionsetMetadata.Options)
         {
           if (optionMetadata.Label.UserLocalizedLabel.Label.ToLower() == optionsetText.ToLower())
            {
                optionSetValue = optionMetadata.Value.Value;
                return optionSetValue;
            }

         }
         return optionSetValue;
   }

  public static string getOptionSetText(string entityName, string attributeName, int optionsetValue)
   {
       string optionsetText = string.Empty;
       RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest();
       retrieveAttributeRequest.EntityLogicalName = entityName;
       retrieveAttributeRequest.LogicalName = attributeName;
       retrieveAttributeRequest.RetrieveAsIfPublished = true;

       RetrieveAttributeResponse retrieveAttributeResponse = 
         (RetrieveAttributeResponse)OrganizationService.Execute(retrieveAttributeRequest);
       PicklistAttributeMetadata picklistAttributeMetadata = 
         (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;

       OptionSetMetadata optionsetMetadata = picklistAttributeMetadata.OptionSet;

       foreach (OptionMetadata optionMetadata in optionsetMetadata.Options)
       {
            if (optionMetadata.Value == optionsetValue)
             {
                optionsetText = optionMetadata.Label.UserLocalizedLabel.Label;
                 return optionsetText;
             }

       }
       return optionsetText;
  }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Comments and Discussions

 
QuestionEntityLogicalName for Global Option Sets??? Pin
GregBoice14-Dec-15 5:26
memberGregBoice14-Dec-15 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 26 Feb 2013
Article Copyright 2013 by Thakkar Amit
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid