Connecting to Dynamics 365 using the Organization Service Proxy

Leave a comment

We will connect to Dynamics 365 from a console app using the Organization Service Proxy.

To do this, create a new console app.

Add the assemblies:

  • Microsoft.Xrm.Sdk
  • System.ServiceModel (for ClientCredentials)

We will also add Microsoft.Crm.Sdk.Proxy to get the version info from Dynamics 365.

Add using statements:

  • using Microsoft.Xrm.Sdk.Client;
  • using System.ServiceModel.Description;
  • using Microsoft.Crm.Sdk.Messages;

Now the code to connect to Dynamics 365:

using System;
using System.Linq;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using Microsoft.Crm.Sdk.Messages;

namespace Carl.Crm.OrgServiceProxy
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Uri oUri = new Uri("https://yourcrm.crm.dynamics.com/XRMServices/2011/Organization.svc");
                ClientCredentials clientCredentials = new ClientCredentials();
                clientCredentials.UserName.UserName = "your@email.com";
                clientCredentials.UserName.Password = "yourpassword";

                OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(oUri, null, clientCredentials, null);
                _serviceProxy.EnableProxyTypes();

                OrganizationServiceContext orgContext = new OrganizationServiceContext(_serviceProxy);

                RetrieveVersionRequest versionRequest = new RetrieveVersionRequest();
                RetrieveVersionResponse versionResponse = (RetrieveVersionResponse)_serviceProxy.Execute(versionRequest);

                Console.WriteLine("Microsoft Dynamics CRM version {0}.", versionResponse.Version);
                Console.ReadLine();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
        }
    }
}

This returns:

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, BI, Web, Cloud and Data Science.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *