Microsoft Dynamics 365 Blog

Introducing CRM MVP Mitch Milam, one of our long time partners, and a very interesting writer too. 

Occasionally, while creating solutions that need to access CRM data, I have found a need to know either the PrimaryField or the PrimaryKey of an Entity.  This information is readily available via the CRM EntityMetadata Class.


Specifies the name of the attribute that is the primary field for the entity.

Specifies the name of the attribute primary key for the entity.  The primary key for a CRM Entity is the Globally Unique Identifier ( GUID ) that CRM uses to reference everything within the CRM system.


The PrimaryKey for account is accountid.

The PrimaryField for account is name.


While developing a solution in .NET, I try and write my methods to be as generic as possible so that I can reuse my code without rewriting the methods each time I implement the code.  This means I use the DynamicEntity functions to a very large extent and those methods, like most CRM Web Service methods, need at least two things to operate: Entity Name and the Entity ID.

But what if I don’t have the Entity ID?  Maybe I’m working with an Import or an Update process and only have the Account Name?

Here is some code that will perform this task:

/// <summary>

/// Retrieve a CRM Entity’s primarykey and primaryfield

/// </summary>

/// <param name=”myMetadataService”></param>

/// <param name=”entityName”></param>

/// <param name=”primaryKey”></param>

/// <param name=”primaryField”></param>

private void GetEntityInfo(

MetadataService myMetadataService,

string entityName,

out string primaryKey,

out string primaryField)


EntityMetadata selectedEntity;

selectedEntity = myMetadataService.RetrieveEntityMetadata(

entityName, EntityFlags.EntityOnly);

primaryKey = selectedEntity.PrimaryKey.ToString();

primaryField = selectedEntity.PrimaryField.ToString();


/// <summary>

/// Return the ID for a CRM Enity record for

/// a given PrimaryField value

/// </summary>

/// <param name=”myCRMService”></param>

/// <param name=”myMetadataService”></param>

/// <param name=”entityName”></param>

/// <param name=”value”></param>

/// <returns></returns>

private Guid FindEntityGuid(

CrmService myCRMService,

MetadataService myMetadataService,

string entityName,

string value)


string idColumnName = string.Empty;

string nameColumnName = string.Empty;

Guid retVal = Guid.Empty;

GetEntityInfo(myMetadataService, entityName,

out idColumnName, out nameColumnName);

// Retrieve privilege by the name.

string formatString =

“<fetch mapping=\”logical\” count=\”1\”>” +

“<entity name=\”{0}\”>” +

“<attribute name='{1}’/>” +

“<attribute name='{2}’/>” +

“<filter type=\”and\”>” +

“<condition attribute=\”{2}\” operator=\”eq\” ” +

“value=\”{3}\”/>” +


string fetchString = string.Format(






// Fetch the results into an xml document.

XmlDocument xmlDoc = new XmlDocument();


XmlNode node = xmlDoc.SelectSingleNode(string.Format(“resultset/result/{0}”,


retVal = new Guid(node.InnerText.ToString());

return retVal;


Using the Code:

The following code will actually exercise the two methods outlined above:

// Connect to the CRM service

CrmService myCRMService = new CrmService();

myCRMService.Credentials = System.Net.CredentialCache.DefaultCredentials;

myCRMService.Url = “http://crm:5555/2006/CrmService.asmx”;

// Connect to the CRM metadata service

MetadataService myMetadataService = new MetadataService();

myMetadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;

myMetadataService.Url = “http://crm:5555/2006/MetadataService.asmx”;

// Find the ID ( Guid ) for the account ‘Microsoft’

Guid myGuid = new Guid();

myGuid = FindEntityRecord(myCRMService, myMetadataService, “account”, “Microsoft”);

// If we actually found the record,

// display the ID ( Guid ).

if (myGuid != Guid.Empty)




Mitch Milam

We're always looking for feedback and would like to hear from you. Please head to the Dynamics 365 Community to start a discussion, ask questions, and tell us what you think!