Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 598
Romeo Avatar asked Apr 27 '14 13:04

Romeo


People also ask

Where the option set field options are stored in CRM?

These are all stored in the StringMapBase table. You'll query via object type code of the entity, attribute name, option set value and language and that'll give you the display value of the attribute.

What is option set in CRM?

An option set is a type of field that can be included in an entity. It defines a set of options. When an option set is displayed in a form it uses a drop-down list control.

How can you configure two option set on form?

Go to your Solution or general Customization in Dynamics 365. Add a new field and select the following properties: Data Type as Option Set. Use Existing Option Set as Yes.

What is entity reference in CRM?

An entity reference is a reference to an entity in a CRM system. The difference between regular fields and entity reference fields is that these are recognized by CRM as pointing to another record in CRM. This is very useful inside CRM.


2 Answers

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();


    }
like image 60
Nicknow Avatar answered Oct 12 '22 23:10

Nicknow


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.

like image 31
Romeo Avatar answered Oct 12 '22 22:10

Romeo