连接 Dynamics 365 Customer Engagement (on-premises)

AuthType = AD

  1. 创建项目模板是.NET framework 4.6.2的控制台程序

  2. 添加nuget包 Microsoft.CrmSdk.CoreAssemblies,Microsoft.CrmSdk.XrmTooling.CoreAssembly

  3. Program类添加以下代码

    using System;
    using System.Configuration;
    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Tooling.Connector;
    namespace ConsoleApp1
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                CrmServiceClient service = new CrmServiceClient(ConfigurationManager.ConnectionStrings["Connect"].ConnectionString);
                // Obtain information about the logged on user from the web service.
                Guid userid = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId;
                var systemUser = service.Retrieve("systemuser", userid,
                    new ColumnSet(new string[] { "firstname", "lastname" }));
                Console.WriteLine("Logged on user is {0} {1}.", systemUser.GetAttributeValue<string>("firstname"), systemUser.GetAttributeValue<string>("lastname"));
    
                // Retrieve the version of Microsoft Dynamics CRM.
                RetrieveVersionRequest versionRequest = new RetrieveVersionRequest();
                RetrieveVersionResponse versionResponse =
                    (RetrieveVersionResponse)service.Execute(versionRequest);
                Console.WriteLine("Microsoft Dynamics CRM version {0}.", versionResponse.Version);
                Console.ReadKey();
            }
        }
    }
    
    
  4. App.config 添加配置

    1. 请根据你的测试环境修改连接字符串中的Domain,Username,Password以及Url

      <?xml version="1.0" encoding="utf-8"?>
      <configuration>
      	<connectionStrings>
      		<add name="Connect" connectionString="
                  AuthType=AD;
      		   Domain=yourDomain;
                  Username=yourUsername;
                  Url=https://contosotest.crm.dynamics.com;
                  Password=yourPassword;"/>
      	</connectionStrings>
          <startup> 
              <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
          </startup>
      </configuration>
      
  5. 运行项目

posted @ 2024-06-04 10:43  Destiny、Yang  阅读(60)  评论(0)    收藏  举报