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

时间:2018-05-07 16:01:57

标签: c# sql dynamics-crm dynamics-365 fetchxml

我知道下面的条件属性=' primaryKey' 不是有效字段,想知道是否有一些提取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());
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

不要过分复杂化。 CRM通过附加&#34; id&#34;从实体架构名称创建主键它适用于所有实体。

例如:帐户为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