如何使用 c# 在 Microsoft Dynamics 365 中查找 guid 的实体名称?

How to find an entity name of a guid in Microsoft Dynamics 365 with c#?

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

这可能是一项罕见且代价高昂的要求 :) 您可以将此代码段用于确切目的。 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);
}

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

  1. 安装XrmToolBox
  2. 安装Universal Search工具里面XrmToolBox
  3. 打开 Universal Search 并在 搜索条件 框中输入您的 GUID
  4. Select 要使用左侧面板搜索的一个或多个实体。由于您知道您的 ID 在 regardingobjectid 字段中,因此您可能 能够将您的搜索限制为 activity 个实体。
  5. 如果这不起作用,您可以使用 检查 All/None 按钮搜索所有实体,但请注意这是一个很长的 运行和缓慢的过程

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

您是如何找到这个 Guid 的?

在 Dynamics 中,您应该永远不会 拥有没有 entity/logical 名称的记录 Guid。他们几乎总是聚在一起——如果你没有看到,那可能不会太远!

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

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

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,则默认情况下应包含注释。

  • 在 model-driven 形式 javascript 中,formContext.getAttribute("regardingobjectid").getValue() 会给你一个 Xrm.LookupValue objects 包含两者。

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

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