Get optionset text from value or value from text

Here are two functions I use on a regular base when working with optionsets in CRM 2011 development projects. When you request an optionset attribute you will get an object of the type “OptionSetValue”. This object contains the value of the selected item. Sometimes this is enough but sometimes you want to retrieve the textual value behind this numeric value. Or sometimes you need to set a certain value in an optionset based on a value. If you then only have the text value, then you need a way to retrieve the numeric value behind that text value. For these common problems I have two methods:

/// <summary>
/// This function is used to retrieve the optionset value using the optionset text label
/// </summary>
/// <param name="service"></param>
/// <param name="entityName"></param>
/// <param name="attributeName"></param>
/// <param name="selectedLabel"></param>
/// <returns></returns>
private int GetOptionsSetValueForLabel(IOrganizationService service, string entityName, string attributeName, string selectedLabel)
{

    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();
    int selectedOptionValue = 0;
    foreach (OptionMetadata oMD in optionList)
    {
        if (oMD.Label.LocalizedLabels[0].Label.ToString().ToLower() == selectedLabel.ToLower())
        {
            selectedOptionValue = oMD.Value.Value;
            break;
        }
    }
    return selectedOptionValue;
}
/// <summary>
/// This function is used to retrieve the optionset labek using the optionset value
/// </summary>
/// <param name="service"></param>
/// <param name="entityName"></param>
/// <param name="attributeName"></param>
/// <param name="selectedValue"></param>
/// <returns></returns>
private string GetOptionsSetTextForValue(IOrganizationService service, string entityName, string attributeName, int selectedValue)
{

    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 = null;
    foreach (OptionMetadata oMD in optionList)
    {
        if (oMD.Value == selectedValue)
        {
            selectedOptionLabel = oMD.Label.LocalizedLabels[0].Label.ToString();
            break;
        }
    }
    return selectedOptionLabel;
}

Comments are closed.