我知道下面的条件属性='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>";
如果你想用通用的最佳实践方式 - Read this。