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

5

6

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 field in an entity.

Oneidaoneil answered 27/4, 2014 at 13:36 Comment(1)
possible duplicate of how to get value/text from a OptionSet?Mayotte
S
13

The proper way to retrieve metadata information in Dynamics CRM is to retrieve only the information required. We should only retrieve the option set values based on the original question. Retrieving all the metadata for an entity when all the requirement specifies is the values for an Option Set is unnecessary and will create unnecessary overhead.

Here is the correct way to get the list of options for an Option Set.

    public static void GetOptionSet(string entityName, string fieldName, IOrganizationService service)
    {

        var attReq = new RetrieveAttributeRequest();
        attReq.EntityLogicalName = entityName;
        attReq.LogicalName = fieldName;
        attReq.RetrieveAsIfPublished = true;

        var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
        var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;

        var optionList = (from o in attMetadata.OptionSet.Options
            select new {Value = o.Value, Text = o.Label.UserLocalizedLabel.Label}).ToList();


    }
Stridulous answered 29/4, 2014 at 1:35 Comment(2)
Thanks @Nicknow. I got to say, this works better than my code. This is what i love in SO, someone can always share a better approach does helping each other to improve their standard in coding.Oneidaoneil
Is their a way to get all option sets?Stage
O
1

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.

Oneidaoneil answered 27/4, 2014 at 13:36 Comment(1)
You are retrieving too much information since you only need the available values you shouldn't need to retrieve all the Entity metadata.Stridulous
B
0

Hey why not use this ??

OptionSetValue CountryOptionSet = Contact.Attributes.Contains("gr_address2_country") ? Contact["gr_address2_country"] as OptionSetValue : null;
 if (CountryOptionSet != null)
                     string Country = CountryOptionSet.Value.ToString();
Boilermaker answered 28/4, 2014 at 5:18 Comment(5)
This will give you the integer value of the field. It will not give you a list of possible Values and their associated Label which is what the original question stated as the requirement.Stridulous
But I am seeing the questions is: How to get the option set from a field in an entity :P he did'nt asked for possible list of values and their associated Label :/Boilermaker
@Dot_Net Pro - When I code "Contact." I do not get the "Attributes" property you have above. What do I need to add so I can get that so I can use your code?Donegal
@Donegal I have Entity Contact = new Entity() Entity have Attributes property.Boilermaker
@Dot_Net Pro: Sorry - I was thrown off by the color of the text - thought your "Contact" was the entity name, not an instance of the Contact entity.Donegal
D
0

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<OptionMetadata> 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

Don answered 17/5, 2016 at 17:10 Comment(0)
T
0

There are two way to get text of OptionSet:

First:

OptionSetValue opProductType = new OptionSetValue();
opProductType = (OptionSetValue)item.Attributes[attributeName];
var optionValue = opProductType.Value;

Second:

var StatusString = TermAndCon.FormattedValues[attributeName].ToString();

(Set) Post OptionSet Value :

newSalesOrder[attributeName] = new OptionSetValue(Convert.ToInt32(optionValue));
Toddy answered 14/2, 2018 at 5:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.