Friday, 15 November 2013

Get Option set text value from CRM 2011 using C#.

public 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;
  }

1 comment: