【问题标题】:Connect to CRM and get data连接到 CRM 并获取数据
【发布时间】:2019-05-29 15:37:12
【问题描述】:

我正在编写一个 ASP.NET 应用程序,它应该连接到动态 CRM 并获取联系人信息。在视图中,它应该返回一个包含他们信息的联系人列表。我提供的测试 CRM 中有一些数据,但我无法获取实体(例如表名及其列),因此我无法在 VS 中创建模型,因为我不知道要使用什么道具放在那里。

有没有办法通过代码获取实体,或者如何解决这个问题?

我尝试过早期绑定生成器,但它不起作用。我尝试使用 ADO.NET 实体数据模型,但也不起作用。

【问题讨论】:

  • 你能直接连接到动态数据库吗?
  • 我可以通过浏览器和 web.config 中的连接字符串连接到 CRM,但我没有任何关于数据库的信息。
  • @justSome_randomGuy 如果您有一个有效的连接字符串,那么您应该拥有连接到数据库并查看 CRM 数据结构所需的信息。
  • 那么你有来自生成器的 OptionSets.cs 和 XRM.cs 吗?您已经创建了到 CRM 服务器的 sqlconnection 并实现了 Create、Retrieve、RetrieveMultiple 和 Update 方法?
  • @DavidYenglin 我没有这些文件,因为generetaor 不起作用。

标签: c# asp.net dynamics-crm crm microsoft-dynamics


【解决方案1】:

为了实际使用 CRM 实体,您需要将项目配置为使用连接到您的 CRM 实例的Organization Service(可能需要不同的教程,具体取决于 CRM 版本)。

之后,就像 David Yenglin 解释的那样,您应该能够使用诸如 IOrganizationService.Retrieve(String, Guid, ColumnSet) 之类的方法,它允许您检索一条记录或 IOrganizationService.RetrieveMultiple(QueryBase),它允许您检索多条记录。在你的情况下,我认为你只需要 Retrieve 方法。

您可以访问数据的另一种方式是通过CRM Web API(我的经验较少),这将允许您从 CRM 访问query the data

在 Microsoft 的 github 存储库 PowerApps-Samples 中有大量不同场景的精彩示例。我建议您稍后在配置项目时检查一下。

【讨论】:

  • 谢谢你的回答,我去看看。
  • @justSome_randomGuy 运气好吗?
  • @justSome_randomGuy 很高兴听到,放轻松
【解决方案2】:

article 将为您提供连接到 CRM 的逐步方法。

一旦您成功连接到 CRM,请尝试以下代码,该代码将为您提供有关所有联系人记录的完整信息。

try
 {
 ClientCredentials clientCredentials = new ClientCredentials();
 clientCredentials.UserName.UserName = "<ProvideUserName>@<ProvideYourOrgName>.onmicrosoft.com";
 clientCredentials.UserName.Password = "<ProvideYourPassword>";

// For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12
 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
// Copy and Paste Organization Service Endpoint Address URL
organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("https://<ProvideYourOrgName>.api.<CRMRegion>.dynamics.com/XRMServices/2011/Organization.svc"),
 null, clientCredentials, null);

if (organizationService != null)
 {
 Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;

if (userid != Guid.Empty)
 {
 Console.WriteLine("Connection Established Successfully...");
 // your logic here.
                    queryExpressionTest(organizationService);


 }
 }
 else
 {
 Console.WriteLine("Failed to Established Connection!!!");
 }
 }
 catch (Exception ex)
 {
 Console.WriteLine("Exception caught - " + ex.Message); 
 }
 Console.ReadKey();
 }
        private static void queryExpressionTest(IOrganizationService organizationService)
        {
            QueryExpression qe = new QueryExpression();
            qe.EntityName = "contact";
            qe.ColumnSet= new ColumnSet(true); // this will give you all the columns of contact record
            //qe.ColumnSet= new ColumnSet("name", "accountnumber"); you could also restrict which particualr attributes you wish to retrieve from contact record.

            EntityCollection coll = organizationService.RetrieveMultiple(qe);
            foreach (Entity cont in coll.Entities)
            {
                Console.WriteLine("Name of contact: " + cont.GetAttributeValue<string>("fullname"));
                Console.WriteLine("Email of contact " + cont.GetAttributeValue<string>("email"));
                /**
                Now you can add all the attributes you wish to show
                */
            }

        }

【讨论】:

    猜你喜欢
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多