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:
[sourcecode language=”c#”]
string fetchXML = ““;
fetchXML += ““;
fetchXML += “
“;

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

And the callback:
[sourcecode language=”c#”]
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”);
}
}
[/sourcecode]
 

Nothing fancy in the helper method:
[sourcecode language=”c#”]
public static object GetValue(Entity entity, string name)
{
if (entity.Attributes.ContainsKey(name))
{
return entity.GetAttributeValue
 
}
return null;
}
[/sourcecode]
 

If I wanted to get the Address 1 Type, I would use:
[sourcecode language=”c#”]
helper.GetValue(entity, “address1_addresstypecode”);
[/sourcecode]
Unfortunately this returns the value (0, 1, 2…) Not the string.

The answer is the Entity.FormattedValues.

Eg
[sourcecode language=”c#”]
entity.GetAttributeValue
[/sourcecode]

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