web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Get Entity Logical Name from its Object Type Code in Dynamics 365

Arpit Shrivastava Profile Picture Arpit Shrivastava 7,518 User Group Leader
Being a CRM developer, while writing the code, you might need to get the Entity Logical Name from its Object Type Code,

So here is the code to do the same:

public static string geEntityLogicalName(int ObjectTypeCode)
{
            string entityLogicalName = String.Empty;

MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And);

EntityFilter.Conditions.Add(
new MetadataConditionExpression("ObjectTypeCode", MetadataConditionOperator.Equals, ObjectTypeCode));

MetadataPropertiesExpression mpe =
new MetadataPropertiesExpression();

mpe.AllProperties =
false;

mpe.PropertyNames.Add(
"ObjectTypeCode");

EntityQueryExpression entityQueryExpression = new EntityQueryExpression()
{

Criteria = EntityFilter,

Properties = mpe

};


RetrieveMetadataChangesResponse initialRequest = GetMetadataChanges(entityQueryExpression, null, DeletedMetadataFilters.OptionSet);

if (initialRequest.EntityMetadata.Count == 1)
{

entityLogicalName = initialRequest.EntityMetadata[0].LogicalName;

}

return entityLogicalName;

}


protected static RetrieveMetadataChangesResponse GetMetadataChanges(EntityQueryExpression entityQueryExpression, String clientVersionStamp, DeletedMetadataFilters deletedMetadataFilter)
{

RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest();
{

Query = entityQueryExpression,

ClientVersionStamp = clientVersionStamp,

DeletedMetadataFilters = deletedMetadataFilter

};


return (RetrieveMetadataChangesResponse)service.Execute(retrieveMetadataChangesRequest);

}

Dynamics 365 Entity Object Type Code List:

Complete Code:

using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using System.ServiceModel.Description;
using System.Net;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Metadata.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

namespace CRMCodeHelper
{
class Program
{
private static IOrganizationService service = null;

static void Main(string[] args)
{
service =
ConnectToCRM();

string entityLogicalName = geEntityLogicalName(112);
            Console.WriteLine("Entity Logical Name is: " + entityLogicalName);       

Console
.ReadLine();
} 


public static string geEntityLogicalName(int ObjectTypeCode)

{

string entityLogicalName = String.Empty;

MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And);

EntityFilter.Conditions.Add(
new MetadataConditionExpression("ObjectTypeCode", MetadataConditionOperator.Equals, ObjectTypeCode));

MetadataPropertiesExpression mpe =
new MetadataPropertiesExpression();

mpe.AllProperties =
false;

mpe.PropertyNames.Add(
"ObjectTypeCode");

EntityQueryExpression entityQueryExpression = new EntityQueryExpression()

{

Criteria = EntityFilter,

Properties = mpe

};

RetrieveMetadataChangesResponse initialRequest = GetMetadataChanges(entityQueryExpression, null, DeletedMetadataFilters.OptionSet);

if (initialRequest.EntityMetadata.Count == 1)

{

entityLogicalName = initialRequest.EntityMetadata[0].LogicalName;

}

return entityLogicalName;

}


protected static RetrieveMetadataChangesResponse GetMetadataChanges(EntityQueryExpression entityQueryExpression, String clientVersionStamp, DeletedMetadataFilters deletedMetadataFilter)

{

RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest()

{

Query = entityQueryExpression,

ClientVersionStamp = clientVersionStamp,

DeletedMetadataFilters = deletedMetadataFilter

};

return (RetrieveMetadataChangesResponse)service.Execute(retrieveMetadataChangesRequest);

}


public static IOrganizationService ConnectToCRM()

{

IOrganizationService organizationService = null;
            try
{

ClientCredentials clientCredentials = new ClientCredentials();

clientCredentials.UserName.UserName =
"<Username>";

clientCredentials.UserName.Password =
"<Password>";

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

organizationService =
new OrganizationServiceProxy(new Uri("https://<organisation name>.crm4.dynamics.com/XRMServices/2011/Organization.svc"),

null, clientCredentials, null);

return organizationService;

}

catch (Exception ex)
{

Console.WriteLine(
"Exception caught - " + ex.Message);

return organizationService;

}

}

}

}
 

Output:

 



This was originally posted here.

Comments

*This post is locked for comments