Retrieving the Text of an OptionSet in Silverlight

I’ve been pulling my hair out over this.

 

If you look at the Dynamics CRM 2011 SDK, you can see how to get an Entity using FetchXML:

Using FetchXML, you can get to an entity like this:


string fetchXML = "<fetch mapping='logical'>";
fetchXML += "<entity name='account'><all-attributes/>";
fetchXML += "</entity></fetch>";

IOrganizationService orgService = OrgServiceFactory.GetInstance();
orgService.BeginRetrieveMultiple(new FetchExpression() { Query = fetchXML }, EntityGet_callback, orgService);

And the callback:


public void EntityGet_callback(IAsyncResult ar)
{
   IOrganizationService orgservice = ar.AsyncState as IOrganizationService;
   var fetchResp = orgservice.EndRetrieveMultiple(ar);
    var entities = fetchResp.Entities;
    foreach (var entity in entities)
    {
        var email = helper.GetValue(entity, "email");
    }
}

 

Nothing fancy in the helper method:


public static object GetValue(Entity entity, string name)
{
    if (entity.Attributes.ContainsKey(name))
    {
        return entity.GetAttributeValue
&amp;nbsp;
    }
    return null;
}

 

If I wanted to get the Address 1 Type, I would use:


helper.GetValue(entity, "address1_addresstypecode");

Unfortunately this returns the value (0, 1, 2…) Not the string.

The answer is the Entity.FormattedValues.

Eg


entity.GetAttributeValue

With thanks to a post on Sayantan Samanta’s blog.