Michael Philip's Blog

Writing about MS Tech with strong focus on Azure & CRM

MS CRM 2011- Retrieve Option Set Items Progamatically Using Organization Service

Leave a comment

Problem:   Suppose you would like to retrieve the items of an option set in the CRM to bind them a drop down in ASP.net or WPF application.

Solution:
use the following code:

      Code:

IOrganizationService _service;
RetrieveAttributeRequest retrieveAttributeRequest =
new RetrieveAttributeRequest
{
EntityLogicalName = “EntityLogicalName”,
LogicalName = “optionSetLogicalName”,
RetrieveAsIfPublished = true,
};

// Execute the request.
RetrieveAttributeResponse retrieveAttributeResponse =    (RetrieveAttributeResponse)_service.Execute(
    retrieveAttributeRequest);

// Access the retrieved attribute.
PicklistAttributeMetadata retrievedPicklistAttributeMetadata =    (PicklistAttributeMetadata)    retrieveAttributeResponse.AttributeMetadata;

// Get the current options list for the retrieved attribute.
OptionMetadata[] optionList =   retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();

Dictionary<int,string> LocalizedLabelDic = new Dictionary<int,string>();

foreach (OptionMetadata o in optionList)
{
LocalizedLabelDic.Add(o.Value.Value,o.Label.LocalizedLabels.FirstOrDefault(e =>          e.LanguageCode == 1033).Label.ToString());
}

Note: I have choose the language code in the query to be 1033 which is English  if your organization has multiple language packs installed and you want to retrieve with a specific language you can view the following link which contains a list of all languages code
http://msdn.microsoft.com/en-us/goglobal/bb964664

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