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

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 =
\"\";

clientCredentials.UserName.Password =
\"\";

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

organizationService =
new OrganizationServiceProxy(new Uri(\"
\"),

null, clientCredentials, null);

return organizationService;

}

catch (Exception ex)
{

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

return organizationService;

}

}

}

}


Output:


Advertisement

Published by arpitpowerguide

My name is Arpit Shrivastava, who is a Microsoft MVP in the Business Applications category. I am a Microsoft Dynamics 365 and Power Platform enthusiast person who is having a passion for researching and learning new things and acquiring immense knowledge. I am providing consistent help, support, and sharing my knowledge through various Social Media Channels along with my Personal Blog, Microsoft Community, conducting online training and attending various 365 Saturday Events worldwide and sharing the best Solutions to the readers helping them achieve their goals and objectives in Customer Relationship Space.

4 thoughts on “Get Entity Logical Name from its Object Type Code in Dynamics 365

  1. Great Article Cloud Computing Projects Networking Projects Final Year Projects for CSE JavaScript Training in Chennai JavaScript Training in Chennai The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: