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;
}

Iterating over requiredattendees in an appointment

In this sample code I will show you how to iterate over the requiredattandees attribute of an appointment. This  attribute is a list of activityparty records. An activityparty record can contain many different entity types, for example contacts and accounts:

proxy.EnableProxyTypes();
//If you want to iterate over every appointment use a RetrieveMumltipleRequest
var appointment = proxy.Retrieve(Appointment.EntityLogicalName,new Guid("FC8C427B-26AE-E111-ADAD-000C29CBD80B"),new ColumnSet(true));
List<ActivityParty> party = ((Appointment)appointment).RequiredAttendees.ToList<ActivityParty>();
foreach (ActivityParty ap in party)
{
    //You can retrieve metadata about a contact or an account by performing a new request
    if(ap.PartyId.LogicalName == Contact.EntityLogicalName)
        Console.WriteLine(string.Format("It's a contact with id {0}",ap.PartyId.Id));
    else if (ap.PartyId.LogicalName == Account.EntityLogicalName)
        Console.WriteLine(string.Format("It's an account with id {0}", ap.PartyId.Id));
}