The Microsoft Dynamics CRM Blog
News and views from the Microsoft Dynamics CRM Team

Understanding CRM Metadata: PrimaryKey and PrimaryField

Understanding CRM Metadata: PrimaryKey and PrimaryField

  • Comments 6

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.

Specifics:

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

PrimaryKey
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.

Example:

The PrimaryKey for account is accountid.

The PrimaryField for account is name.

Uses:

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}\"/>" +

"</filter></entity></fetch>";

string fetchString = string.Format(

formatString,

entityName,

idColumnName,

nameColumnName,

value);

// Fetch the results into an xml document.

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(myCRMService.Fetch(fetchString));

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

idColumnName));

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)

{

MessageBox.Show(myGuid.ToString());

}

Mitch Milam

  • PingBack from http://blogs.infinite-x.net/2007/06/18/my-guest-post-on-the-dynamics-crm-team-blog/

  • Hi,

    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?

    thankx

    Divya

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

    should probably read FindEntityGuid?

    David

    MCTS

  • 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

  • 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.  

  • Hello

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

    Jon

    jkksanders@tiscali.co.uk

Page 1 of 1 (6 items)
Leave a Comment
  • Please add 1 and 7 and type the answer here:
  • Post