Tuesday, December 13, 2011

Retrieve OptionSet Text in CRM 2011 using C#

Hi All, In CRM 2011 we have two types of Option Sets. one will be normal Option set Filed and the other one will be Global Option Set. Lets see how to retrieve Option Set Text for both types.

Retrieve Normal(Local) Option Set Text


// Get Normal option set Text
string optionsetText = entity.FormattedValues["new_optionset"];

or

string optionsetText = entity.GetFormattedAttributeValue("new_optionset");

Retrieve Global Option Set Text

int OptionsetValue = ((Microsoft.Xrm.Sdk.OptionSetValue)entity["new_localoptionset"]).Value;
string GlobaloptionsetText= GetOptionsetText(entity, service, "new_globaloptionset", OptionsetValue );



  // Retrieves Global Option set Selected Text
        // Parameters: 1. Entity Name   2. Service  3. Global Option Set Name   4. optionset selected value
        public string GetOptionsetText(Entity entity, IOrganizationService service,string  optionsetName,int optionsetValue)
        {
            string optionsetSelectedText = string.Empty;
            try
            {


                RetrieveOptionSetRequest retrieveOptionSetRequest =
                    new RetrieveOptionSetRequest
                    {
                        Name = optionsetName
                    };

                // Execute the request.
                RetrieveOptionSetResponse retrieveOptionSetResponse =
                    (RetrieveOptionSetResponse)service.Execute(retrieveOptionSetRequest);

                // Access the retrieved OptionSetMetadata.
                OptionSetMetadata retrievedOptionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;

                // Get the current options list for the retrieved attribute.
                OptionMetadata[] optionList = retrievedOptionSetMetadata.Options.ToArray();
                foreach (OptionMetadata optionMetadata in optionList)
                {
                    if (optionMetadata.Value == optionsetValue)
                    {
                        optionsetSelectedText = optionMetadata.Label.UserLocalizedLabel.Label.ToString();
break;
                    }
                }
            }
            catch (Exception)
            {


                throw;
            }
            return optionsetSelectedText;
        }

===========================================


// Gets Local/Normal optionset Lable using Metadata Service
         public static string GetoptionsetText(string entityName, string attributeName, int optionSetValue, IOrganizationService service)
         {
             string AttributeName = attributeName;
             string EntityLogicalName = entityName;
             RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
             {
                 EntityFilters = EntityFilters.All,
                 LogicalName = EntityLogicalName
             };
             RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
             Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
             Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals

(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
             Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet;
             IList<OptionMetadata> OptionsList = (from o in options.Options
                                                     where o.Value.Value == optionSetValue
                                                     select o).ToList();
             string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
             return optionsetLabel;
         }




happy coding :) 

12 comments:

  1. Thank you Guru! Your code helped me with this OptionSet nightmare!

    ReplyDelete
  2. This is great! How would you get the lable for a Two Options field type? IOW, if the value is 0, how do you know if the label is False, No, Empty, etc?

    ReplyDelete
  3. Gary! you can use Metadata service request to retrieve any attribute data. we can get Label of Two option filed Label as well, i did not tried it.

    ReplyDelete
  4. Hi Guru,

    Can you please let us know how to retrieve the Text of TwoOptions?

    ReplyDelete
    Replies
    1. Hi Pradeep,

      use the following code to get two options field Text

      // Get Normal two option set Text
      string twoOptionLable = entity.FormattedValues["new_twoOptionSet"];

      Let me know if any issues..

      Delete
  5. And how can I retrieve optionset's selected value for record into variable from its Post Image in Plugin created on Update event ?

    ReplyDelete
    Replies
    1. var optvalue = PostImage.Attributes["new_optionset"];

      Delete
  6. Hi Guru,

    Account has accountratingcode optionset. I want to retrieve value of that optionset when accountratingcode is on unassigned.

    ReplyDelete
    Replies
    1. Hi Ravideep,

      you can use the following code to get the optionset value inside the plugin
      var optvalue = entity.Attributes["accountratingcode"];

      I did not get what exactly your requirement. provide me more information where you wants to get optionset value?(plugin,javascript etc.) so that i can help you.

      Thanks,
      Guru

      Delete
    2. How to retrieve and update Two Option Option set?

      Delete
    3. use the following code to retrieve two option set text

      // Get Normal two option set Text
      string twoOptionLable = entity.FormattedValues["new_twoOptionSet"];

      //to update two option field
      entity.Attributes.Add("new_bitfield", true);

      Hope it helps!

      Delete