Logo
banner

Blogs

Load metadata of all entities and get Object Type Code based on Entity name in Dynamics 365 for Sales.

, August 12, 2021 3682 Views

In this blog I’ll be taking you over how to get the Object type Code for the particular entity based on the Entity name. Suppose we need to get object type code for different entities in a loop in an stand alone .Net application. Generally, people are writing a fetch into the loop but not keeping in mind that this is causing a performance issue as the connection of Dynamics 365 is opening and closing many time based on the loop. And while checking on google, we can get a code to find object type code for an entity itself by using a condition in metadata query. To resolve this issue, I have come up with a solution like first we get the Object type Code for all the entities once, store the same in generic list of Entity type and find the ObjectType Code from List itself whenever needed.

Please note: We are assuming that you already created the object of Organization service.

Below is the code that you can use to implement this.

Get the metadata of all the Entities.

#region GetEntityMetadataCollection
    /// <summary>
    /// Returns the entities metadata into the collection for the further use.
    /// </summary>
    /// <param name="organizationService">Object of IOrganizationService to get the data.</param>
    private static List<EntityMetadata> GetEntityMetadataCollection(IOrganizationService organizationService)
    {
      List<EntityMetadata> lstEntityMetadata = new List<EntityMetadata>();
      try
      {
        MetadataFilterExpression metadataFilterExpression = new MetadataFilterExpression(LogicalOperator.And);
        MetadataPropertiesExpression metadataPropertiesExpression = new MetadataPropertiesExpression { AllProperties = false };
        metadataPropertiesExpression.PropertyNames.Add("LogicalName");
        metadataPropertiesExpression.PropertyNames.Add("ObjectTypeCode");
        EntityQueryExpression entityQueryExpression = new EntityQueryExpression();
        entityQueryExpression.Criteria = metadataFilterExpression;
        entityQueryExpression.Properties = metadataPropertiesExpression;

        RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest();
        retrieveMetadataChangesRequest.Query = entityQueryExpression;

        RetrieveMetadataChangesResponse retrieveMetadataChangesResponse = (RetrieveMetadataChangesResponse)organizationService.Execute(retrieveMetadataChangesRequest);

        if (retrieveMetadataChangesResponse.EntityMetadata != null)
        { lstEntityMetadata.AddRange(retrieveMetadataChangesResponse.EntityMetadata); }
      }
      catch (Exception ex)
      {
        throw ex;
      }

      return lstEntityMetadata;
    }
    #endregion

Get the specific entity object type code from the metadata.

Create new method with below code.

#region GetEntityObjectTypeCode
   /// <summary>
   /// Gets the entity object type code form the metadata for the specific entity.
   /// </summary>
   /// <param name="entityLogicalName">Entity logical name to get the entity object type.</param>
   /// <param name="lstEntityMetadata">Collection of entities metadata to get the entity object type code.</param>
   /// <returns></returns>
   private static int? GetEntityObjectTypeCode(string entityLogicalName, List<EntityMetadata> lstEntityMetadata)
   {
     int? entityObjectTypeCode = null;

     try
     {
       if (lstEntityMetadata != null &&
           string.IsNullOrWhiteSpace(entityLogicalName) == false)
       {
         EntityMetadata foundEntityMetadata = lstEntityMetadata.Find(entityMetadata => entityMetadata.LogicalName.Equals(entityLogicalName, StringComparison.CurrentCultureIgnoreCase));
         if (foundEntityMetadata != null)
         { entityObjectTypeCode = foundEntityMetadata.ObjectTypeCode; }
       }
     }
     catch
     {
     }
     return entityObjectTypeCode;
   }
   #endregion

Calling these methods from Main class.

List<EntityMetadata> lstEntityMetadata = GetEntityMetadataCollection(organizationService);
       int? accountEntityObjectTypeCode = GetEntityObjectTypeCode("account", lstEntityMetadata);

Below are the required namespace.

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Metadata.Query;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Text;

Hope this helps!

 


ATM Inspection PowerApp to ease ATM inspection and report generation process.
https://www.inkeysolutions.com/microsoft-power-platform/power-app/atm-inspection

Insert data into Many-to-Many relationship in Dynamics CRM very easily & quickly, using the Drag and drop listbox.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/drag-and-drop-listbox

Comply your Lead, Contact, and User entities of D365 CRM with GDPR compliance using the GDPR add-on.
https://www.inkeysolutions.com/microsoft-dynamics-365/dynamicscrmaddons/gdpr

Create a personal / system view in Dynamics CRM with all the fields on the form/s which you select for a particular entity using the View Creator.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/view-creator

 

mm

Inkey

INKEY is your solution partner.
Our focus is to deliver you in-time intelligent innovative solutions ("key") for the problems in hand. Maintaining a quality standard right from the inception of a project is our top most priority.

Our team of talented professionals will execute your projects with dedication and excellence. We take ownership and accountability for the effort that goes into meeting our client’s needs.

Years of experience and proven success of delivering innovative custom solutions.

More posts by

Leave a Reply

Your email address will not be published. Required fields are marked *

The maximum upload file size: 2 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Drop file here

Would you like to digitize your business and put it on the cloud?
Do you need clear, concise reports for your organization?