web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How can I get entity logicalName from objecttypecode?

(0) ShareShare
ReportReport
Posted on by

Hi, may I ask is there any ways that I can fetch entity logicalname by providing the objecttypecode?

Thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    BharatPremji Profile Picture
    2,485 on at

    Hi,

    You can use the Metadata Document Generator in XRMToolBox :

    http://www.xrmtoolbox.com/

    This can output the entities of your choice into an Excel document and give you the entity type code and other useful information

    Regards

  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    6,804 on at

    Hi cowboy@arlington,

    I found some code from MVP Scott Durow's "SharePoint Integration from CRM Online Workflow/Plugin" which includes methods to fetch entity logical name from ObjectTypeCode.

    Original site is here.  The purpose of the original code was to parse DynamicUrl to get the Entity Reference.

    The class DynamicUrlParser includes Property EntityTypeCode (aka ObjectTypeCode).

    public class DynamicUrlParser
    {
        public string Url { get; set; }
        public int? EntityTypeCode { get; set; }
        public Guid? Id { get; set; }
    
    ....
    
    }

    There's a method in the DynamicUrlParser called GetEntityLogicalName.  This method gets the EntityLogicalName from EntityTypeCode.

    /// <summary> 
    /// Find the Logical Name from the entity type code - this needs a reference to the Organization Service to look up metadata 
    /// </summary> 
    /// <param name="service"></param> 
    /// <returns></returns> 
    public string GetEntityLogicalName(IOrganizationService service)
    {
        var entityFilter = new MetadataFilterExpression(LogicalOperator.And);
        entityFilter.Conditions.Add(new MetadataConditionExpression("ObjectTypeCode ", MetadataConditionOperator.Equals, this.EntityTypeCode));
        var propertyExpression = new MetadataPropertiesExpression { AllProperties = false };
        propertyExpression.PropertyNames.Add("LogicalName");
        var entityQueryExpression = new EntityQueryExpression()
        {
            Criteria = entityFilter,
            Properties = propertyExpression
        };
    
        var retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest()
        {
            Query = entityQueryExpression
        };
    
        var response = (RetrieveMetadataChangesResponse)service.Execute(retrieveMetadataChangesRequest);
    
        if (response.EntityMetadata.Count == 1)
        {
            return response.EntityMetadata[0].LogicalName;
        }
        return null;
    }
    

    That should do the trick.  Hope this helps.

    Cheers,

    Nadeeja

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at

    https://customer.crm.dynamics.com/api/data/v8.1/EntityDefinitions?$select=ObjectTypeCode&$filter=SchemaName eq 'Account'

    will give back the following json result:

    {

     "@odata.context":"https://customer.crm.dynamics.com/api/data/v8.1/$metadata#EntityDefinitions(ObjectTypeCode)","value":[

       {

         "ObjectTypeCode":1,"MetadataId":"70816501-edb9-4740-a16c-6a5efbc05d84"

       }

     ]

    }

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at

    Actually you need this:

    https://customer.crm.dynamics.com/api/data/v8.1/EntityDefinitions?$select=SchemaName,ObjectTypeCode&$filter=ObjectTypeCode eq 1

    results in:

    {

    "@odata.context":"https://customer.crm.dynamics.com/api/data/v8.1/$metadata#EntityDefinitions(SchemaName,ObjectTypeCode)","value":[

    {

    "SchemaName":"Account","ObjectTypeCode":1,"MetadataId":"70816501-edb9-4740-a16c-6a5efbc05d84"

    }

    ]

    }

  • Community Member Profile Picture
    on at

    Hi Andreas,

    I am getting following error when I try to access using the https://url/api/data/v8.2/EntityDefinitions

    I have dynamic 365 on premises version

    {

     "error":{

       "code":"","message":"An unexpected error occurred.","innererror":{

         "message":"An unexpected error occurred.","type":"System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]","stacktrace":"   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, ExecutionContext executionContext)\r\n   at Microsoft.Crm.Extensibility.OData.CrmODataMetadataServiceDataProvider.RetrieveMetadataForEntityType(CrmODataExecutionContext context, Type crmType, EntityQueryExpression entityQuery, String entityId, String crmPropertyName)\r\n   at Microsoft.Crm.Extensibility.OData.CrmODataMetadataServiceDataProvider.RetrieveEdmEntityCollection(CrmODataExecutionContext context, String entitySetName, String castEntityName, ODataQueryOptions queryOptions)\r\n   at Microsoft.Crm.Extensibility.OData.EntityController.GetEntitySetInternal(String entitySetName, String castEntityName, CrmODataExecutionContext context, CrmEdmEntityObjectCollection crmEdmEntityObjectCollection, ODataQueryOptions queryOptions)\r\n   at Microsoft.Crm.Extensibility.OData.EntityController.GetEntitySet(String entitySetName)\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"

       }

     }

    }

    please any help or suggest

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at

    Hi Vivek,

    I would recommend to use PowerShell to activate tracing on your server to check and find out what is going wrong.

    support.microsoft.com/.../how-to-enable-tracing-in-microsoft-dynamics-crm

    If you create a new vanilla organization and open this link, do you also get this error?

    Cheers,

    Andreas

  • Community Member Profile Picture
    on at

    If anyone wants to just pull it with SQL you can grab it from the EntityView view.

    SELECT OriginalLocalizedName, LogicalName, ObjectTypeCode
    
     FROM [CHANGEME-DB Name].[dbo].[EntityView]
    
    ORDER BY OriginalLocalizedName, LogicalName asc


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans