Retrieve the text label for an OptionSet value

Here is the implementation I use for a method to get back the text/caption of an OptionSet item.

private  string GetOptionsSetTextOnValue(IOrganizationService service, string entityName, string attributeName, intselectedValue)
{
RetrieveAttributeRequest retrieveAttributeRequest = new  RetrieveAttributeRequest {
EntityLogicalName = entityName,
LogicalName = attributeName,
RetrieveAsIfPublished = true
};
// Execute the request.
RetrieveAttributeResponse retrieveAttributeResponse =(RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
// Access the retrieved attribute.
Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata retrievedPicklistAttributeMetadata =(Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)   retrieveAttributeResponse.AttributeMetadata;
// Get the current options list for the retrieved attribute.
OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
string selectedOptionLabel = string.Empty;
foreach (OptionMetadata oMD in optionList)
{
if (oMD.Value == selectedValue)
{
selectedOptionLabel = oMD.Label.UserLocalizedLabel.Label;
}
}
return selectedOptionLabel;
Advertisements
This entry was posted in CRM SDK, Helpers, Plugins, Uncategorized and tagged , . Bookmark the permalink.

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