Connection to CRM

旧街凉风 提交于 2019-11-29 07:29:46

You should use the ►Simplified Connection provided by the ►Microsoft.Xrm.Client.CrmConnection type for an easier experience.

Steps are easy:

1) in the .config file add a connection string

<connectionStrings>
  <add name="CrmConnStr" connectionString="!SEE EBELOW!"/>
</connectionStrings>

2) In code, it goes like this:

// 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);

About the connection string, if On-Premise WITHOUT IFD it should be

"Url=http[s]://serverurl/organization; Domain=DOMAIN; Username=USERNAME; Password=PASSWORD"
<!-- https is always nice, but it's not mandatory in this case -->

If On-Premise WITH IFD or Online it should be

"Url=https://org.server.url; Username=USERNAME; Password=PASSWORD"
<!-- https is of course mandatory here -->
<!-- 'DOMAIN=...' part is not needed because of ADFS -->
Shashank Nandoju

Use these assemblies in your project:

using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Client.Services;

Create the organization service:

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);

Don't forget to import 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();

try this: Default Credentials are used,using Windows Authentication

ClientCredentials Credentials = new ClientCredentials();
Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

//This URL needs to be updated to match the servername and Organization for the environment.

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;
  }

use Microsoft.Xrm.Tooling.Connector

    var service =new CrmServiceClient(
                "AuthType=Office365;Username=username; Password=password;Url=https://url");

Not specifying the Organization Name may be the trouble of not authenticating correctly.. it should be something like:

http://myserver:myport/OrganizationName/XRMServices/2011/Organization.svc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!