连接到CRM
我需要我的网站连接到CRM这是我的代码
var organizationUri = new Uri(ConfigurationManager.AppSettings["CRMServerUrl"]); //Client credentials var credentials = new ClientCredentials(); credentials.UserName.UserName = @""; credentials.UserName.Password = ""; // Use the Microsoft Dynamics CRM Online connection string from the web.config file named "CrmConnectionStr". using (OrganizationServiceProxy _service = new OrganizationServiceProxy(organizationUri, null, credentials, null)) { Response.Write("Connected"); }
这是在我的web.config中
<add key="username" value="" /> <add key="password" value="" />
它给了我这个错误信息:
“发生了严重错误。无法与CRM服务器连接。调用者未通过该服务进行身份validation。”
您应该使用►Microsoft.Xrm.Client.CrmConnection类型提供的►SimplifiedConnection,以获得更轻松的体验。
步骤很简单:
1)在.config文件中添加一个连接字符串
2)在代码中,它是这样的:
// parameter is the name of the connection string // NOTE: These "setup" declarations are slow, reuse them as much as possibile var connection = new CrmConnection("CrmConnStr"); var service = new OrganizationService(connection); var context = new CrmOrganizationServiceContext(connection);
关于连接字符串,如果On-Premise没有IFD它应该是
"Url=http[s]://serverurl/organization; Domain=DOMAIN; Username=USERNAME; Password=PASSWORD"
如果使用IFD或在线进行内部部署则应该是
"Url=https://org.server.url; Username=USERNAME; Password=PASSWORD"
在项目中使用这些程序集:
using Microsoft.Xrm.Client; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Client.Services;
创建组织服务:
string connectionString = "Url=https://your_orgnisation.crm5.dynamics.com; Username=user_name; Password=your_password;"; CrmConnection connection = CrmConnection.Parse(connectionString); OrganizationService organisationservice = new OrganizationService(connection);
不要忘记导入System.Runtime.serialization
。
By looking at your code, you are not using username and password
Change below lines:
//Client credentials var credentials = new ClientCredentials(); credentials.UserName.UserName =ConfigurationManager.AppSettings["username"].toString(); credentials.UserName.Password =ConfigurationManager.AppSettings["password"].toString();
试试这个: Default Credentials are used,using Windows Authentication
ClientCredentials Credentials = new ClientCredentials(); Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
//需要更新此URL以匹配环境的服务器名称和组织。
Uri OrganizationUri = new Uri("http://XXXXX/XRMServices/2011/Organization.svc"); Uri HomeRealmUri = null; using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null)) { IOrganizationService service = (IOrganizationService)serviceProxy; }
使用Microsoft.Xrm.Tooling.Connector
var service =new CrmServiceClient( "AuthType=Office365;Username=username; Password=password;Url=https://url");
不指定组织名称可能是未正确validation的麻烦..它应该是这样的:
HTTP:// MYSERVER:MyPort上/单位名称/ XRMServices / 2011 / Organization.svc