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

后端 未结 5 600
闹比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:15

    depends if it's local or global. If it is local then:

    string optionsetText = entity.FormattedValues["new_optionset"];
    

    if its global then you need a lot more code:

            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 OptionsList = (from o in options.Options
                                                 where o.Value.Value == optionSetValue
                                                 select o).ToList();
            string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
            return optionsetLabel;
    

    I got that from Guido on another similar question but rcados is missing the 3rd parameter. To get the text you just set

    var foo = GetoptionsetText("lead", "picklist", 1234 , service)
    

    assuming you have already declared IorganizationService

提交回复
热议问题