How to get Meta Data for All entities in CRM 2011 using MS CRM 2011 API

Posted on Posted in CRM 2011 SDK, MS CRM 2011, MS Dynamics, MS Dynamics CRM

How to get Meta Data for All entities in CRM 2011 using MS CRM 2011 API
We need to create RetrieveAllEntitiesRequest first


RetrieveAllEntitiesRequest entityRequest = new RetrieveAllEntitiesRequest();

Then call the service.execute()

We need to cast result into RetrieveAllEntitiesResponse.

RetrieveAllEntitiesResponse allEntities = (RetrieveAllEntitiesResponse)service.Execute(entityRequest);

Once we have result back we need to save them as EntityMetadata.
var entityMetadata = allEntities.EntityMetadata;

above code can be wrapped inside a method EntitiesMetadata(orgName)
From this meta data we can get list of all objects in org and do further work

Following method will create object types map for an org
static private void CreateObjectTypeMaps(string orgName)
{

if (objectTypeMaps == null)
{
lock (Crm2011Services.SyncTypes)
{
objectTypeMaps = new Dictionary<string, Dictionary<string, int>>();
}
}

if (!objectTypeMaps.ContainsKey(orgName))
{
lock (Crm2011Services.SyncTypes)
{
Dictionary<string, int> objectTypeCodes = new Dictionary<string, int>();

foreach (EntityMetadata em in EntitiesMetadata(orgName))
{
objectTypeCodes.Add(em.LogicalName, em.ObjectTypeCode.Value);
}
objectTypeMaps[orgName] = objectTypeCodes;
}
}
}

We can then use method below to get list of Object Type Codes by passing an org name and entity name
static public string EntityNameFromTypeCode(string orgName, int typeCode)
{
CreateObjectTypeMaps(orgName);
Dictionary<string, int> objectTypeCodes = objectTypeMaps[orgName];
foreach (string entityName in objectTypeCodes.Keys)
{
if (objectTypeCodes[entityName] == typeCode)
{
return entityName;
}
}
return null;
}

———————————————————-Full code

1)
///
/// Get entity name by passing entity code
///

static public string EntityNameFromTypeCode(string orgName, int typeCode)
{
CreateObjectTypeMaps(orgName);
Dictionary<string, int> objectTypeCodes = objectTypeMaps[orgName];
foreach (string entityName in objectTypeCodes.Keys)
{
if (objectTypeCodes[entityName] == typeCode)
{
return entityName;
}
}
return null;
}

2)
static Dictionary<string, Dictionary<string, int>> objectTypeMaps = null;
private static SyncObject SyncTypes = new SyncObject();

static private void CreateObjectTypeMaps(string orgName)
{

if (objectTypeMaps == null)
{
lock (Crm2011Services.SyncTypes)
{
objectTypeMaps = new Dictionary<string, Dictionary<string, int>>();
}
}

if (!objectTypeMaps.ContainsKey(orgName))
{
lock (Crm2011Services.SyncTypes)
{
Dictionary<string, int> objectTypeCodes = new Dictionary<string, int>();

foreach (EntityMetadata em in EntitiesMetadata(orgName))
{
objectTypeCodes.Add(em.LogicalName, em.ObjectTypeCode.Value);
}
objectTypeMaps[orgName] = objectTypeCodes;
}
}
}

3)
///
/// to get meta data of all entities in organization
///

////// list of meta data = {Microsoft.Xrm.Sdk.Metadata.EntityMetadata[]}
static private EntityMetadata[] EntitiesMetadata(string orgname)
{
try
{
using (OrganizationServiceProxy service = CrmServerConnection.GetOrganisationProxy(orgname))
{

RetrieveAllEntitiesRequest entityRequest = new RetrieveAllEntitiesRequest();

RetrieveAllEntitiesResponse allEntities = (RetrieveAllEntitiesResponse)service.Execute(entityRequest);

return allEntities.EntityMetadata;
}
}
catch (System.Exception)
{
return null;
}
}