在没有名称的主键上获取 XML 过滤器

Fetch XML filter on Primary Key without name

我知道下面的 condition attribute='primaryKey' 不是有效字段,想知道是否有一些 fetch xml 语法允许您在不知道键名的情况下过滤主键?

[TestMethod]
public async System.Threading.Tasks.Task ShoulRetrieveAnyEntity(OrganizationServiceProxy _oragnizationServiceProxy)
{
    var entityName = "account";
    var entityGuid = "7004d3c1-3147-e811-a95e-000d3a10877d";

    string xml = "<fetch distinct='false' version='1.0' output-format='xml-platform' mapping='logical' no-lock='true'>" +
                     "<entity name='" + entityName + "'>" +
                        "<all-attributes />" +
                            "<filter type='and'>" +
                                "<condition attribute='primaryKey' operator='eq' value='{" + entityGuid + "}' />" +
                            "</filter>" +
                        "</entity>" +
                    "</fetch>";

    RetrieveMultipleRequest rmRequest = new RetrieveMultipleRequest() { Query = new FetchExpression(xml) };
    EntityCollection eResults = ((RetrieveMultipleResponse)_oragnizationServiceProxy.Execute(rmRequest)).EntityCollection;
    if (eResults.Entities.Count > 0)
    {
        foreach (KeyValuePair<string, object> attribute in eResults.Entities[0].Attributes)
            {
                Console.WriteLine(attribute.Key + ": " + attribute.Value.ToString());
            }
        }
    }
}

不要把它复杂化。 CRM 通过为所有实体附加 "id" 从 实体架构名称 创建 主键

例如:帐户是 accountid,机会是 opportunityid,等等

string xml = "<fetch distinct='false' version='1.0' output-format='xml-platform' mapping='logical' no-lock='true'>" +
                     "<entity name='" + entityName + "'>" +
                        "<all-attributes />" +
                            "<filter type='and'>" +
                                "<condition attribute='" + entityName + "id' operator='eq' value='{" + entityGuid + "}' />" +
                            "</filter>" +
                        "</entity>" +
                    "</fetch>";

仍然,如果您想以通用的最佳实践方式进行操作 -