我正在从控制台应用程序查询MS Dynamics CRM Online:
public EntityCollection GetEntities(string entityName)
{
IOrganizationService proxy = ServerConnection.GetOrganizationProxy();
string request = string.Format("<fetch mapping ='logical'><entity name = '{0}'></entity></fetch>", entityName);
FetchExpression expression = new FetchExpression(request);
var mult = proxy.RetrieveMultiple(expression);
return mult;
}
此代码最多只返回中的5000个元素mult.Entities
。我知道CRM中还有更多实体。如何检索所有实体?
使用提取XML一次只能获取5000条记录。
要获取更多记录,您必须使用分页cookie,请参见此处:
相关的代码位:
// Define the fetch attributes.
// Set the number of records per page to retrieve.
int fetchCount = 3;
// Initialize the page number.
int pageNumber = 1;
// Specify the current paging cookie. For retrieving the first page,
// pagingCookie should be null.
string pagingCookie = null;
修改了主循环,因为该示例似乎并未更新分页cookie:
while (true)
{
// Build fetchXml string with the placeholders.
string xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);
FetchExpression expression = new FetchExpression(xml);
var results = proxy.RetrieveMultiple(expression);
// * Build up results here *
// Check for morerecords, if it returns 1.
if (results.MoreRecords)
{
// Increment the page number to retrieve the next page.
pageNumber++;
pagingCookie = results.PagingCookie;
}
else
{
// If no more records in the result nodes, exit the loop.
break;
}
}
我个人倾向于使用LINQ而不是FetchXML,但是值得注意的是Lasse V. Karlsen所说的,如果要向用户提供此信息,则可能要进行某种分页(在FetchXML或LINQ中)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句