Retrieve Option Set Selected Text Value in CRM 2011

To get an in-dept understanding Option Sets , you can check the SDK in this location:

SampleCode\CS\Metadata\OptionSets\WorkWithOptionSets.cs

This sample shows how to perform the following actions with global option sets:

  • Create, retrieve, or update a global option set.
  • Create a PicklistAttributeMetadata attribute using a global option set.
  • Insert or update an option item.
  • Re-order option items.
  • Retrieve all global option sets.

Deepak Kumar in his post has nicely talked about how to retrieve Option Set Selected Text Value in CRM 2011.

GuruPrasad has also exposed the code here.

This piece of code  would really help !

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

 

Advertisements

About mytechlifedays

Its been a wonderful learning period over the last 6 years in the IT industry , getting exposed to whole lot of technologies and ideas. The hurdles and the crisis that came along have been wonderful experience ... And now its time to pen them down so that let some others execute faster and easily with these information .....
This entry was posted in MS CRM and tagged , . Bookmark the permalink.

Leave a Reply ! It would be always appreciated ! :)

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