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:
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;
}
}
}
}
It is much helpful article about stop test cases. Your blog is having interesting topic as compared to other article on the web. PHP Services
ReplyDeleteEcommerce website Development Company
Magento Service Providers
Data Extraction Services
Online Payment Gateway
CRM Services
Bulk Appointments registers
Online Appointment Scheduling Software