Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

Thakkar Amit
Technical Lead
United States United States
Amit Thakkar is having total work experience over 13 years in implementing projects using various Microsoft Technologies like MS CRM 2011/4.0, Asp.net, C#, MOSS 2007, SSRS and SSIS.
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 26 Feb 2013
Article Copyright 2013 by Thakkar Amit
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid