How to get the option set from a field in an entity in CRM 2011 using crm sdk and C#? I just want to share with you guys a direct approach on getting the option set of a fie
This method needs the entity name, name of the field which contain the option set and the instantiated IOrganizationService.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
public void GetOptionSet(string entityName, string fieldName, IOrganizationService service)
{
RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest();
retrieveDetails.EntityFilters = EntityFilters.All;
retrieveDetails.LogicalName = entityName;
RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, fieldName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata;
OptionSetMetadata options = picklistMetadata.OptionSet;
var optionlist = (from o in options.Options
select new { Value = o.Value, Text = o.Label.UserLocalizedLabel.Label }).ToList();
//from here you can do anything you want now with the optionlist
}
Reference:
http://guruprasadcrm.blogspot.ae/2011/12/retrieve-optionset-text-in-crm-2011.html
I hope this will help some of you guys with your project.