1.连接crm方法
public class OrganisationServiceHelper
{
private string _userName;
private string _password;
private string _domain;
private string _url;
public static IServiceManagement<IOrganizationService> sm;
public IOrganizationService _organizationService;
public OrganisationServiceHelper(string userName, string password, string domain, string url)
{
_userName = userName;
_password = password;
_domain = domain;
_url = url;
_organizationService = CrmConnection(userName, password, domain, url);
}
public OrganizationServiceProxy CrmConnection(string userName, string password, string domain, string url)
{
try
{
//从Web.config读取连接CRM组织的WebService链接
string CRMServiceUrl = url;
sm = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(CRMServiceUrl));
ClientCredentials credentials = new ClientCredentials();
//初始化连接参数
credentials.UserName.UserName = userName + "@" + domain;
credentials.UserName.Password = password;
//创建连接
OrganizationServiceProxy crmConnection = new OrganizationServiceProxy(sm, credentials);
return crmConnection;
}
catch (Exception ex)
{
throw ex;
}
}
//(另一种方式)
private IOrganizationService GetOrganisationService(string userName, string password, string domain, string url)
{
try
{
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new NetworkCredential(userName, password, domain);
OrganizationServiceProxy proxy = new OrganizationServiceProxy(new Uri(url), null, credentials, null);
proxy.ClientCredentials.UserName.UserName = userName;
proxy.ClientCredentials.UserName.Password = password;
return proxy as IOrganizationService;
}
catch (Exception ex)
{
throw ex;
}
}
}