MSCrm CrmDiscoveryService

 CrmDiscoveryService是MS Crm中一个战略层次的服务, 它允许调用者确定在Crm中隶属的组织以及用来访问它们的Url, 我们可以用一个请求(Request)来让 CrmDiscovery Service 返回一个调用者隶属组织的列表.
        以下是Active Directory (On-Premise)方式的认证
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. //using CrmSdk.Discovery namespace.
  5. //using CrmSdk;
  6. using System.Net;
  7. using VAR.utility;
  8. namespace discoverysample
  9. {
  10.     using CrmSdk.Discovery;
  11.     class Program
  12.     {
  13.         
  14.         static void Main(string[] args)
  15.         {
  16.             //Create a instance of CrmDiscoveryService
  17.             try
  18.             {
  19.                 utility.log(@"please configure app.config before run the application.");
  20.                 
  21.                 utility.log("Create CrmDiscoveryService start");
  22.                 discoverysample.CrmSdk.Discovery.CrmDiscoveryService discovery = new discoverysample.CrmSdk.Discovery.CrmDiscoveryService();
  23.                 //default we use the default credential
  24.                 utility.log("Create CrmDiscoveryService end");
  25.                 //discovery.UseDefaultCredentials = true;
  26.                 //discovery.Credentials = System.Net.CredentialCache.DefaultCredentials;
  27.                 //set the url we'll access.
  28.                 utility.log("get local configure");
  29.                 string hostname = System.Configuration.ConfigurationSettings.AppSettings["hostname"];
  30.                 utility.log("hostname:"+hostname );
  31.                 string port = System.Configuration.ConfigurationSettings.AppSettings["port"];
  32.                 utility.log("port:" + port);
  33.                 string authtype = System.Configuration.ConfigurationSettings.AppSettings["authenticationtype"];
  34.                 utility.log("authtype:" + authtype);
  35.                 discovery.Url = String.Format("http://{0}:{1}/MSCRMServices/2007/{2}/CrmDiscoveryService.asmx", hostname, port, authtype);
  36.                 utility.log("CrmDiscoveryServiceUrl:" + discovery.Url);
  37.                 
  38.                 
  39.                 Console.Write("username:");
  40.                 string username = Console.ReadLine();
  41.                 Console.Write("password:");
  42.                 string password = utility.ReadPassword();
  43.                 utility.log("");
  44.                 discovery.Credentials = new System.Net.NetworkCredential(username, password);
  45.                 discovery.PreAuthenticate = true;
  46.                 
  47.                 //discovery.Credentials = new NetworkCredential(username, password);
  48.                 utility.log("Request OrganizationDetails information ");
  49.                 // Retrieve the organization name and endpoint Url from the CrmDiscoveryService Web service.
  50.                 RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
  51.                 RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)discovery.Execute(orgRequest);
  52.                 utility.log("retrieve OrganizationDetails information");
  53.                 OrganizationDetail orgInfo = null;
  54.                 
  55.                 // Iterate through all the retrieved organizations
  56.                 foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
  57.                 {
  58.                     System.Reflection.PropertyInfo [] props= orgDetail.GetType().GetProperties();
  59.                     foreach (System.Reflection.PropertyInfo propInfo in props)
  60.                     { 
  61.                         utility.log("    "+propInfo.Name +":"+propInfo.GetValue (orgDetail ,null));
  62.                     }
  63.                     
  64.                 }
  65.                 
  66.                 
  67.                 utility.log("sample complete!");
  68.                
  69.             }
  70.             catch (System.Web.Services.Protocols.SoapException ex)
  71.             {
  72.                 utility.log(ex.Detail.InnerText);
  73.             }
  74.             catch (Exception ex)
  75.             {
  76.                 utility.log(ex.Message);
  77.             }
  78.             Console.Read();
  79.         }
  80.     }
  81. }
    以下是ActiveDirectory (SPLA)的认证模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace VAR.MSCrm.CrmTraining
  5. {
  6.     using VAR.MSCrm.Service.CrmReference.CrmSdk ;
  7.     using VAR.MSCrm.Service.CrmReference.CrmSdk.Discovery ;
  8.     using VAR.MSCrm.Utility.CrmServiceWrapper ;
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.              //Create a instance of CrmDiscoveryService
  14.             try
  15.             {
  16.                 
  17.                 utility.log(@"please configure app.config before run the application.");
  18.                 //default we use the default credential
  19.                 
  20.                 //discovery.UseDefaultCredentials = true;
  21.                 //set the url we'll access.
  22.                  utility.log("get local configure");
  23.                 string server = System.Configuration.ConfigurationSettings.AppSettings["server"];
  24.                  utility.log("server:{0}",server );
  25.                 string domain = System.Configuration.ConfigurationSettings.AppSettings["domain"];
  26.                 utility.log("domain:{0}",domain  );
  27.                 string organizationName = System.Configuration.ConfigurationSettings.AppSettings["organizationName"];
  28.                 utility.log("organizationName:{0}",organizationName  );
  29.                 string userName = System.Configuration.ConfigurationSettings.AppSettings["userName"];
  30.                 utility.log("userName:{0}",userName  );
  31.                 string authenticationtype = System.Configuration.ConfigurationSettings.AppSettings["authenticationtype"];
  32.                 utility.log("authtype:{0}" , authenticationtype);
  33.                 utility.log("create discovery service");
  34.                 CrmDicoveryServiceWrapper discovery = new CrmDicoveryServiceWrapper(server, authenticationtype);
  35.                 utility.log("discovery serviced create completed");
  36.                
  37.                 
  38.                 string password = utility.CapturePassword("please input your password:");
  39.                 
  40.             
  41.                 
  42.                 //discovery.Credentials = new System.Net.NetworkCredential (username ,password,domain );
  43.                 //discovery.PreAuthenticate = true;
  44.                 
  45.                 //discovery.Credentials = new NetworkCredential(username, password);
  46.                 utility.log("retrevie organizations");
  47.                 // Retrieve the organization name and endpoint Url from the CrmDiscoveryService Web service.
  48.                 RetrieveOrganizationsRequest  orgRequest = new RetrieveOrganizationsRequest();
  49.                 // Substitute an appropriate domain, username, and password here.
  50.                 orgRequest.UserId = domain + "//" + userName;
  51.                 orgRequest.Password = password;
  52.                 RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)discovery.Execute(orgRequest);
  53.                 utility.log("list organizations:");
  54.                 OrganizationDetail orgInfo = null;
  55.                 
  56.                 // Iterate through all the retrieved organizations
  57.                 foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
  58.                 {
  59.                     System.Reflection.PropertyInfo [] props= orgDetail.GetType().GetProperties();
  60.                     foreach (System.Reflection.PropertyInfo propInfo in props)
  61.                     { 
  62.                         utility.log("    "+propInfo.Name +":"+propInfo.GetValue (orgDetail ,null));
  63.                     }
  64.                    
  65.                 }
  66.                 
  67.                 
  68.                 utility.log("sample complete!");
  69.                
  70.             }
  71.             catch (System.Web.Services.Protocols.SoapException ex)
  72.             {
  73.                 utility.log(ex.Detail.InnerText);
  74.             }
  75.             catch (Exception ex)
  76.             {
  77.                 utility.log(ex.Message);
  78.             }
  79.             Console.Read();
  80.         
  81.         }
  82.     }
  83. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值