How to Retrieve OptionSets Label Text in Dynamics CRM 2011

Calling Below Method as

string labelValue = GetOptionSetValueLabel(CrmService, accEntity.LogicalName, "homeroof" , 100001);

// Method to retrieve OptionSet label value
Private string GetOptionSetValueLabel(IOrganizationService CrmWebService, string prmEntityName, string prmAttributeName, int prmOptionSetValue)
{
string ReturnLabel = string.Empty;

OptionMetadataCollection OptionsSetLabels = null;

OptionsSetLabels = RetrieveOptionSetMetaDataCollection(ref CrmWebService, prmEntityName, prmAttributeName);

foreach (OptionMetadata OptionMetdaData in OptionsSetLabels)
{
if (OptionMetdaData.Value == prmOptionSetValue)
{
ReturnLabel = OptionMetdaData.Label.UserLocalizedLabel.Label;
break;
}
}

return ReturnLabel;
}

// Method to Retrieve OptionSet Options Metada Data Collection.
Private OptionMetadataCollection RetrieveOptionSetMetaDataCollection( IOrganizationService CrmWebService, string prmEntityName, string prmAttributeName)
{

OptionMetadataCollection ReturnOptionsCollection = null;
RetrieveEntityRequest RetrieveEntityRequest = new RetrieveEntityRequest();
RetrieveEntityResponse RetrieveEntityResponse = new RetrieveEntityResponse();

RetrieveEntityRequest.LogicalName = prmEntityName;

RetrieveEntityRequest.EntityFilters = Microsoft.Xrm.Sdk.Metadata.EntityFilters.Attributes;

RetrieveEntityResponse = (RetrieveEntityResponse)CrmWebService.Execute(RetrieveEntityRequest);

foreach (AttributeMetadata AttributeMetadata in RetrieveEntityResponse.EntityMetadata.Attributes)
{
if (AttributeMetadata.AttributeType == AttributeTypeCode.Picklist &&
AttributeMetadata.LogicalName == prmAttributeName)
{
ReturnOptionsCollection = ((PicklistAttributeMetadata)AttributeMetadata).OptionSet.Options;
break;
}
}

return ReturnOptionsCollection;
}
This entry was posted in MS CRM 2011 and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *