【问题标题】:How to find an entity name of a guid in Microsoft Dynamics 365 with c#?如何使用 c# 在 Microsoft Dynamics 365 中查找 guid 的实体名称?
【发布时间】:2021-08-17 10:33:14
【问题描述】:

我有一个 guid,但我不知道它是针对哪个实体的。 如何找到拥有的实体名称? 例如,有一个 guid 代码:7487cd8b-a0a2-eb11-b81e-005056a460ec。但是实体名称是什么?

【问题讨论】:

  • 为什么您认为 GUID 是实体 ID?你在哪里找到它?这个问题太笼统了。
  • 我知道!我想找到一个在 aboutobjectid 字段中的 guid。所以这是一个 guid 代码,但我不知道在 aboutobject 字段中使用了哪个实体。
  • 当您有一个regardingobjectid 时,您在同一行中也有一个regardingobjecttypecode。先看看那里。

标签: c# dynamics-crm microsoft-dynamics dynamics-365


【解决方案1】:

如果您不想自己编写 GUID 搜索,可以使用 XrmToolBox 工具 Universal Search。您输入一个 GUID 并跨实体搜索。

  1. 安装XrmToolBox
  2. 安装Universal Search工具内部XrmToolBox
  3. 打开Universal Search 并在搜索条件框中输入您的 GUID
  4. 使用左侧面板选择一个或多个要搜索的实体。由于您知道自己的 ID 在 aboutobjectid 字段中,因此您可能可以将搜索限制为活动实体。
  5. 如果这不起作用,您可以使用 Check All/None 按钮搜索所有实体,但请注意这是一个耗时且缓慢的过程

Universal Search XrmToolBox tool with annotations(低代表所以不能发布内嵌图片)

【讨论】:

    【解决方案2】:

    您是如何发现这个指南的?

    在 Dynamics 中,您应该绝不拥有没有实体/逻辑名称的记录 Guid。它们几乎总是聚在一起——如果你没有看到它,它可能不会太远!

    • 在 C# Dynamics SDK 中,查找字段应始终为 EntityReference 对象。

    • (最棘手的)如果您使用 Web API HTTP 请求,请确保在您的请求标头中 include annotations

    Accept: application/json  
    Content-Type: application/json; charset=utf-8  
    OData-MaxVersion: 4.0  
    OData-Version: 4.0  
    Prefer: odata.include-annotations="*"
    

    这将包括其他字段:

    _regardingobjectid_value@Microsoft.Dynamics.CRM.lookuplogicalname:"new_myentity",
    _regardingobjectid_value:"ad2d51c9-1de2-449d-b80a-76232503aacf",
    
    • 如果使用 Xrm.WebAPI,则默认情况下应包含注释。

    • 在模型驱动的表单 javascript 中,formContext.getAttribute("regardingobjectid").getValue() 将为您提供一个包含两者的 Xrm.LookupValue 对象数组。

    entityType: "new_myentity", id: "{C8FE3743-2730-4625-85D5-325837015D8B}", name: "Name"}
    

    在相关说明中,我认为永远不要将 guid 放入变量、参数或其他内容中是​​个好主意——正如您所发现的那样,它们本身毫无意义——总是随身携带 EntityReference / Xrm.LookupValue 对象。

    【讨论】:

      【解决方案3】:

      这可能是一个罕见且昂贵的要求 :) 您可以将此代码 sn-p 用于确切目的。 Thanks to "Guid"o

      // Retrieve Metadata for all entities
      RetrieveAllEntitiesRequest allEntitiesRequest = new RetrieveAllEntitiesRequest();
      allEntitiesRequest.EntityFilters = EntityFilters.Entity;
      allEntitiesRequest.RetrieveAsIfPublished = true;
      RetrieveAllEntitiesResponse allEntitiesResponse = (RetrieveAllEntitiesResponse)service.Execute(allEntitiesRequest);
      
      // Record ID to check
      Guid checkId = new Guid("541067bd-4db1-2208-e5b0-a601ead56d03");
      
      string entityLogicalName = string.Empty;
      foreach (EntityMetadata entityMetadata in allEntitiesResponse.EntityMetadata)
      {
          try
          {
              // Try to retrieve the record from the current entity
              Entity test = service.Retrieve(entityMetadata.LogicalName, checkId, new ColumnSet(entityMetadata.PrimaryIdAttribute));
              // if the previous instruction didn't throw an exception means that the record exists.
              entityLogicalName = entityMetadata.LogicalName;
              break;
          }
          catch { }
      }
      if (entityLogicalName != string.Empty)
      {
          string foundMessage = string.Format("Record with ID {0} belongs to entity {1}", checkId.ToString(), entityLogicalName);
          Console.WriteLine(foundMessage);
      }
      else
      {
          string notFoundMessage = string.Format("Record with ID {0} not found in any entity", checkId.ToString());
          Console.WriteLine(notFoundMessage);
      }
      

      【讨论】:

      • 感谢您的回答,阿伦。但正如你所说,这是一种非常公开且昂贵的方式。也许没有更简单的方法可以找到这个问题。
      • @DavoodRamezani 也许您可以尝试使用人类可读的唯一编号(例如 con-12345 用于联系人等)替代密钥......而不是 guid
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多