Saturday, 15 April 2017

How to connect to Dynamics CRM 2016 Online and On Premise using a C# console application

With the release of the CRM 2016 SDK, came a much needed yet underrated update – Xrm.Tooling.Connector.
The team at Microsoft has simplified connections to CRM to the point of being a developer’s dream, no need for Http bindings, Service References, or countless debugging attempts to get a C# Console application or a ASP.NET Portal to connect to Dynamics CRM.
How to use it –
1 – The app.config file from the SDK example contains a sample of each connection type, make sure you have the correct connectionstring added to your app.config.
    <!-- Online using Office 365 -->
    <!-- <add name="Server=CRM Online, organization=contoso, user=someone" connectionString="Url=https://contoso.crm.dynamics.com; Username=someone@contoso.onmicrosoft.com; Password=password; authtype=Office365"/> -->
    
    <!-- On-premises with provided user credentials -->
    <!-- <add name="Server=myserver, organization=AdventureWorksCycle, user=administrator" connectionString="Url=http://myserver/AdventureWorksCycle; Domain=mydomain; Username=administrator; Password=password; authtype=AD"/> -->

    <!-- On-premises using Windows integrated security -->
    <!-- <add name="Server=myserver, organization=AdventureWorksCycle" connectionString="Url=http://myserver/AdventureWorksCycle; authtype=AD"/> -->

    <!-- On-Premises (IFD) with claims -->
   
 <!--<add name="Server=litware.com, organization=contoso, user=someone@litware.com" connectionString="Url=https://contoso.litware.com; Username=someone@litware.com; Password=password; authtype=IFD"/>-->
**These connection strings are read as an array for the ConfigurationManager
2 – In your Console application,
string connStr = ConfigurationManager.ConnectionStrings[0].ConnectionString;
3. To use this connection string
              // Establish a connection to the organization web service.
                Println("Connecting to Online using O365...");

                // Connect to the CRM web service using a connection string.
                CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connStr);

                // Cast the proxy client to the IOrganizationService interface.
                //_orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
                IOrganizationService service = (IOrganizationService)conn.OrganizationServiceProxy;
                Print("connected");
                
                // Obtain information about the logged on user from the web service.
                Guid userid = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId;
                Entity systemUser = (Entity)service.Retrieve("systemuser", userid,
                    new ColumnSet(new string[] { "firstname", "lastname" }));
                Println("Logged on user is " + systemUser.Attributes["firstname"].ToString() + " " + systemUser.Attributes["lastname"].ToString() + ".");

                // Retrieve the version of Microsoft Dynamics CRM.
                RetrieveVersionRequest versionRequest = new RetrieveVersionRequest();
                RetrieveVersionResponse versionResponse =
                    (RetrieveVersionResponse)service.Execute(versionRequest);
                Println("Microsoft Dynamics CRM version " + versionResponse.Version + ".");
And Finally, don’t forget to add the assembly reference –
using Microsoft.Xrm.Tooling.Connector;

No comments:

Post a Comment