Retrieve Option Set Selected Text Value in CRM 2011
In CRM 4.0 it was very easy to get the Picklist Selected Text value. But in CRM 2011 the Picklist has been changed to Option Set and interestingly they have not exposed any Property to get Text value of the selected value.
So now we can get the value only from the Metadata. It’s not huge but simple to implement. Below is the method which returns the Selected Text Value on Passing the Integer Value.
public string GetPickListText(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> picklistOption = (from o in options.Options where o.Value.Value == optionSetValue select o).ToList(); string picklistLabel = (picklistOption.First()).Label.UserLocalizedLabel.Label; return picklistLabel; }
Now by using this we can easily get the selected option set value of any attribute of any record. Now to retrieve the text value we need to pass the value of the option set.
For e.g [How to call] To Get the selected text for Shipping method for a contact record
IOrganizationService service = GetCrmService();//Get the Crm Service string textValue=GetPickListText("contact","address1_shippingmethodcode",5,service) ;
I hope this really helps. HAPPY Reading !!!!!
#1 by mytechlifedays on March 21, 2012 - 11:56 am
Thanks a lot! It helped indeed !