Thursday, 31 January 2019

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

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

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                organizationService = new OrganizationServiceProxy(new Uri("<CRM Organisation Service URL>"),

                    null, clientCredentials, null);

                return organizationService;

            }

            catch (Exception ex)
            {

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

                return organizationService;

            }

        }

    }

}


Output:




1 comment:

Blogger Widgets