2016-09-12 72 views
0

我是Dynamics CRM中的新成員,我想創建一個控制檯應用程序,可以爲帳戶實體創建新記錄,並可以顯示來自Dynamics CRM在線帳戶實體的所有帳戶名稱的列表。如何使用控制檯應用程序從Dynamics CRM Online中檢索數據?

這是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Configuration; 
using Microsoft.Xrm.Sdk; 
using Microsoft.Xrm.Tooling.Connector; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int choice; 
      CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString); 
      IOrganizationService crmService = crmConn.OrganizationServiceProxy; 

      Entity acc = new Entity("account"); 
      String account_name; 
      Console.WriteLine("Press 1 to Create a new account or Press 2 to view list of available accounts."); 
      choice = Convert.ToInt32(Console.ReadLine()); 
      switch (choice) 
      { 
       case 1: 
        Console.WriteLine("Enter Name of Account to Create ?"); 
        account_name = Console.ReadLine(); 
        acc["name"] = account_name; 
        crmService.Create(acc); 
        Console.WriteLine("*****An account with name {0} is created successfully*****", account_name); 
        Console.WriteLine(); 
        Console.WriteLine("Press any key to exit.."); 
        Console.ReadKey(); 
        break; 
       case 2: 
        //code to display list of all account names in CRM. 
        Console.ReadKey(); 
        break; 
       default: 
        Console.WriteLine("Wrong input..."); 
        Console.ReadKey(); 
        break; 

      } 
     } 

    } 
} 
+0

如果你想檢索數據,你需要做一個QueryExpression –

+0

QueryExpression查詢=新QueryExpression {實體名稱= 「帳戶」,ColumnSet =新ColumnSet(新的String [] { 「名」}) }; EntityCollection account = service.RetrieveMultiple(query); 是否這樣? – Dev

+1

是的,就像那樣。 –

回答

1

這裏是我的回答

內部案例2:我用下面的代碼:

QueryExpression query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet(new string[] { "name" }) }; 
EntityCollection account = crmService.RetrieveMultiple(query); 
string name = ""; 
foreach (var count in account.Entities) 
{ 
    name = count.GetAttributeValue<string>("name"); 
    Console.WriteLine(name); 
} 
Console.ReadKey(); 
0

對於案例2我會更好地去使用FetchXML的方法。轉到高級查找並獲取過濾條件,然後下載xml文件。

然後納入fetchxml在你的代碼和檢索這樣

VAR objCollection數據= crmService.RetrieveMultiple(新FetchExpression(fetchXMLString));

希望它可以幫助

+0

FetchXML限制5000條記錄,但我的客戶端有5000條以上的記錄。無論如何工作現在完成。謝謝你的回答@Manish – Dev

相關問題