This is pretty straight forward, and I will show you two methods for this.
First you need to add references to your project.
You will need to add the Microsoft.Xrm.Sdk and Microsoft.Xrm.Tooling.Connector assembly references to your project.
Within the console application, add the following to the namespace declaration area (the commented lines can be used if you need to access query, metadata or messages):
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
// using Microsoft.Xrm.Sdk.Metadata;
// using Microsoft.Xrm.Sdk.Messages;
// using Microsoft.Xrm.Sdk.Query;
Add a global variable to your class for the Organization Service:
// Not required if you decide to use CrmServiceClient only
private IOrganizationService _orgService;
Option 1 is to connect using a connection string:
In your App.Config add the following configuration:
<connectionStrings>
<add name="Server=domain.com, organization=myorg, user=crmadmin@domain.local"
connectionString="Url=crminternal.domain.com/.../Organization.svc; Username=domain\crmadmin; Password=CRM_PASSWORD; authtype=IFD"/>
</connectionStrings>
string connectionString = ConfigurationManager.ConnectionStrings["name"].ConnectionString;
CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString);
Option 2 is to connect using individual application settings:
In your App.Config add the following application settings:
<appSettings>
<add key="UserName" value="domain\crmadmin"/>
<add key="Password" value="CRM_PASSWORD" />
<add key="InternalUrl" value="crminternal.domain.com"/>
<add key="OrgName" value="myorg"/>
</appSettings>
string userName = ConfigurationManager.AppSettings["UserName"].ToString();
string password = ConfigurationManager.AppSettings["Password"].ToString();
string internalUrl = ConfigurationManager.AppSettings["InternalUrl"].ToString();
string orgName = ConfigurationManager.AppSettings["OrgName"].ToString();
NetworkCredential creds = new NetworkCredential(userName, ConvertToSecureString(password));
Microsoft.Xrm.Tooling.Connector.AuthenticationType authType = Microsoft.Xrm.Tooling.Connector.AuthenticationType.IFD;
CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(creds, authType, internalUrl, "443", orgName, true, true, null);
That's basically it.
You can check if the connection is ready by using the following statement:
if (conn.IsReady) ...
If you want to use the Organization Service:
_orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
You should be able to get it working now.
Hope this helps.