Search code examples
dynamics-crmdynamics-crm-2015

Unable to get name from EntityReference


I'm using CRM 2015 SDK for my plugin. I want the attributes of entity reference in my code. I'm able to get the Guid and Logical Name. But the name returns null for all the entity reference fields. Here is my code:

EntityReference centre= ((EntityReference)quoteEntity.Attributes["mc_centre"]);
Guid centreGuid = centre.Id; //returns Guid
string centreName = centre.Name; //returns null

I have checked the Referenced Entity, "Centre" which uses the 'name' field and has valid value. Has anyone faced the same issue? Am I doing something wrong in my code? I don't want another service call to get the name btw.


Solution

  • the Name property of an EntityReference is not always populated when you cast it from an attribute. You need to do an additional retrieve if you want to get the name.

    This is the relevant MSDN article: EntityReference.Name Property

    This property can contain a value or null. This property is not automatically populated unless the EntityReference object has been retrieved from the server.