How to get the option set from a field in an entity in CRM 2011 using crm sdk and C#

后端 未结 5 587
闹比i
闹比i 2020-12-18 09:05

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

5条回答
  •  再見小時候
    2020-12-18 09:22

    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.

提交回复
热议问题