连接 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 @   Destiny、Yang  阅读(21)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示