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.
#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
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
List<EntityMetadata> lstEntityMetadata = GetEntityMetadataCollection(organizationService); int? accountEntityObjectTypeCode = GetEntityObjectTypeCode("account", lstEntityMetadata);
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
© All Rights Reserved. Inkey IT Solutions Pvt. Ltd. 2024
Leave a Reply