Understanding CRM Metadata: PrimaryKey and PrimaryField

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

Comments (7)

  1. Divya says:


    I want the display name of attributes of a particular entity to be listed in dropdown down list using meatadata service in .net.Can anybody help me in coding.How to get the display name of attributes in a particular entity and how to use retrieveentitymetadata function in sdk?



  2. David says:

    Mild confusion but otherwise a very useful post, in "Using the code" the line myGuid = FindEntityRecord(myCRMService, myMetadataService, "account", "Microsoft");

    should probably read FindEntityGuid?



  3. Har du någon gång funderat på det? Vilken nytta kan du ha av det och vad skall du använda de till? Här

  4. Bill Smith says:

    It seems to me that importing accounts/contacts, contacts/activities etc etc is such a fundamental step that we need a way that the poor importer (often NOT a developer) can do this without having to figure out how to "get" an entity’s GUID.

    So if we have already imported the accounts, then the only thing we should have to do with the "primary customer" field in the contact record source, would be to put "account, Pepsi Cola Corp" in the source "account name" field – and the import would know to go access an already loaded "account" entity record and put the link in the newly loaded contact record.  

  5. Jonathan Sanders says:


    i’m wanting to know how i can shrink the "Metadata" file size on a user profile? many thanks.



  6. DD says:


    I have configured CRM11 with metadata service in my code. Now I need to support CRM 2013 and CRM 2015. What are the changes should be made regarding this?

Skip to main content