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

问题描述 投票:1回答:1

我知道下面的条件属性='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());
            }
        }
    }
}
c# sql dynamics-crm dynamics-365 fetchxml
1个回答
1
投票

不要过度复杂化。 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>";

如果你想用通用的最佳实践方式 - Read this

© www.soinside.com 2019 - 2024. All rights reserved.