2014-11-05 32 views
4

取5000個多實體,我從我的控制檯應用程序,查詢MS Dynamics CRM Online中:如何從CRM

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; 
} 

此代碼僅返回最大的mult.Entities 5000個的元素。我知道CRM中有更多的實體。 如何檢索所有實體?

+1

你爲什麼要查詢這麼多記錄?當然你不會向用戶展示超過5000行來查看/選擇? – 2014-11-05 11:01:18

+2

當您想從CRM中以編程方式將數據下載到自定義應用程序的數據庫中時,可能會出現這種情況。 – PhantomReference 2016-05-11 14:18:15

回答

8

您只能使用抓取XML一次取回5000條記錄。

爲了獲得更多的記錄,你必須使用尋呼的cookie,在這裏看到:

Sample: Use FetchXML with a paging 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中)

+0

本例中的代碼有pagingCookie = null。 在結果的第二頁,此代碼將拋出OrganizationServiceFault異常「嘗試檢索任何高頁面上的一組記錄時,需要調頁cookie。」 – Tschareck 2014-11-05 11:17:10

+0

MSDN示例似乎沒有更新分頁Cookie。它應該作爲從RetrieveMultiple返回的結果的屬性提供 – Joe 2014-11-05 11:52:22

+0

如果您的結果集包含多個頁面,則此代碼將起作用。 if(result.MoreRecords)將是true,如果結果中有比第一頁更多的記錄,然後您將設置pagingcookie。如果您從此代碼中收到錯誤,請嘗試執行調試以瞭解爲什麼您無法獲得分頁Cookie。上週我自己使用了這個例子,它爲我工作,當然你需要一個合理的結果集。祝你好運! – 2014-11-07 08:13:18

4

您可以使用LINQ,如下所示。 CRM LINQ提供程序將自動分頁查詢並根據需要運行儘可能多的請求以獲取完整結果,並將完整集返回到單個對象中。對我們來說真的很方便。但是,請小心。如果結果集非常大,它將會明顯變慢,甚至在極端情況下甚至會拋出OutOfMemoryException。

public List<Entity> GetEntities(string entityName) 
    { 
     OrganizationServiceContext DataContext = new OrganizationServiceContext(ServerConnection.GetOrganizationProxy()); 

     return DataContext.CreateQuery(entityName).toList(); 
    } 

這裏有一個替代實現分頁與FetchXML,我喜歡比官方的例子好得多:

int page = 1; 
EntityCollection entityList = new EntityCollection(); 

do 
{ 
    entityList = Context.RetrieveMultiple(new FetchExpression(String.Format("<fetch version='1.0' page='{1}' paging-cookie='{0}' count='1000' output-format='xml-platform' mapping='logical' distinct='false'> ... </fetch>", SecurityElement.Escape(entityList.PagingCookie), page++))); 

    // Do something with the results here 
} 
while (entityList.MoreRecords); 
0

我有同樣的問題。我通過在提取xml或Query Expression中包含實體的id來解決它。如果您在查詢中包含「lead」的傳遞,那麼還要添加「leadid」。然後,分頁cookie將自動爲您生成。