Retrieve Option Set Metadata in Microsoft Dynamics CRM 2011

Posted: October 2, 2012 in Code Snippets, CRM 2011

Here is the code to retrieve Option Set metadata in CRM 2011. You need to pass the optionset value to get the select text.

public static string GetOptionsSetTextOnValue(IOrganizationService service, string entitySchemaName, string attributeSchemaName, int optionsetValue)
        {
            RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest
            {
                EntityLogicalName = entitySchemaName,
                LogicalName = attributeSchemaName,
                RetrieveAsIfPublished = true
            };
            RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
            PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
            OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
            string metadata = string.Empty;
            if (optionList.Length > 0)
            {
                metadata = (from a in optionList
                            where a.Value == optionsetValue
                            select a.Label.UserLocalizedLabel.Label).First();
            }
            return metadata;
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s