The Simplest Connection to Dynamics365

Whenever starting something new, you always want to find the quickest way to make something happen.

Think Hello World – I want Hello World (but not really Hello World) so I can start doing all that cool stuff.

I was searching the other day for some code on how to make an initial connection to Dynamics365 for the start of a new project.

Here were the objectives;

  1. I didn’t want to download someone else’s library on creating a connection.
  2. I wanted to learn how to connect to Dynamics365.

The first point is really important because although there are some really, really great libraries out there, it prevents the accomplishment of my second objective which was – I wanted to write the code, not consume someone else’s code.

The best way to learn how something really works is always to do it yourself.

So here it is, in all it’s unrefined, unchecked, (definitely not hardened and validated across multiple scenarios) glory, the simplest code to connect to Dynamics365.  All you need is to download the Dynamics365 SDK and add a reference Microsoft.Xrm.Sdk.

ClientCredentials credentials = new ClientCredentials();
 credentials.UserName.UserName = user;
 credentials.UserName.Password = password;

try
 {
 string ServerUri = String.Format("{0}://{1}/XRMServices/2011/Organization.svc", protocol, Server);
 Uri serviceUri = new Uri(ServerUri);

OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
 IOrganizationService _CrmService = (IOrganizationService)proxy;
 }
 catch (Exception ex)
 {
 System.Diagnostics.Debug.WriteLine(LogFormatter.FormatError(ex));
 }

And now we harden it, make it better and stronger.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s