2016-07-26 76 views
0

我正在嘗試使控制檯應用程序檢索使用Dynamics CRM SDK和C#的結果,但我似乎無法從我的查詢中獲得任何結果。我能夠看到我已連接到服務器,但我試圖做的任何QueryExpression似乎都沒有任何迴應,即使我沒有設置過濾器。即使使用文檔中的示例,我們也有相應的值,最終我會空手而歸。我的代碼是:CRM RetrieveMultipleResponse不返回任何結果

 CrmServiceClient crmSvc = new CrmServiceClient(ConfigurationManager.ConnectionStrings["MyCRMServer"].ConnectionString); 
     Console.WriteLine(crmSvc.IsReady); 
     //Display the CRM version number and org name that you are connected to. 
     Console.WriteLine("Connected to CRM! (Version: {0}; Org: {1}", 
     crmSvc.ConnectedOrgVersion, crmSvc.ConnectedOrgUniqueName); 

     QueryExpression userSettingsQuery = new QueryExpression("contact"); 
     userSettingsQuery.ColumnSet.AllColumns = true; 
     var retrieveRequest = new RetrieveMultipleRequest() 
     { 
      Query = userSettingsQuery 
     }; 
     EntityCollection EntCol = (crmSvc.ExecuteCrmOrganizationRequest(retrieveRequest) as RetrieveMultipleResponse).EntityCollection; 
     foreach (var a in EntCol.Entities) 
     { 
      Console.WriteLine("Account name: {0} {1}", a.Attributes["firstname"], a.Attributes["lastname"]); 
     } 
     Console.Write(crmSvc.LastCrmError); 
     Console.Write(crmSvc.LastCrmException); 
     Console.ReadLine(); 

它返回任何錯誤,並顯示出真正的連接,我似乎無法找到從哪裏開始從這裏解決。

回答

1

您的代碼適合我,所以您可能因爲連接的用戶沒有權限讀取您的CRM組織中的contact記錄或沒有contact記錄。另外,我知道你正在拼湊一個樣本,但是我在下面彙集了一個更直接的代碼版本。

var crmSvc = new CrmServiceClient(<CONNECTION STRING>); 

    var contactQuery = new QueryExpression("contact") 
    { 
     ColumnSet = new ColumnSet("firstname", "lastname") 
    }; 
    var contacts = crmSvc.RetrieveMultiple(contactQuery).Entities; 
    foreach(var contact in contacts) 
    { 
     Console.WriteLine("Contact name: {0} {1}", contact.GetAttributeValue<String>("firstname"), contact.GetAttributeValue<String>("lastname")); 
    } 
+0

感謝您改進的代碼,這有點拼湊在一起的代碼後,我得到了很多不同的方式來試圖讓它工作。我必須和我的CRM部門進行溝通,因爲我過去曾經有權限問題。謝謝。 – Greg2518