Softstuff Consulting
My occasional musings of a technical nature
  • Send us a message
  • About Softstuff Consulting
  • Privacy Policy
  • Main Website
C# , Dynamics365

Updating your CRM V9 connection code

by Ian Blair February 23, 2019 No Comments
I had a client ring me in a bit of a panic as their CRM system had upgraded to V9 over the weekend and the forms integration I had built for them ages ago had stopped working. They kept getting a ‘Connection forcibly closed’ error every time they tried to submit a form.
Fortunately I had recoded the connection part when they went to Dynamics365 from CRM2016 so it looked like this.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Tooling.Connector;
using System.ServiceModel;
var connectionString = "url=https://mycrmsystem; Username=myusername; Password=mypassword; authtype=Office365";
CrmServiceClient conn = new CrmServiceClient(connectionString);
using (OrganizationServiceProxy orgService = conn.OrganizationServiceProxy) {
     if (conn.IsReady) {
          // code to do stuff goes here
      }else{
         throw new invalidoperationexception(conn.LastCRMError);
      }
}

To make it work with CRM V9 turned out to be quite simple. Firstly I had to install .NET v 4.6 on the server that was running the code, previously it was on 4.5.2. Next I had to install the new versions of the Microsoft.Xrm.Sdk.dll, and the Microsoft.Xrm.Tooling.Connector from NuGet as there doesn’t seem to be the a huge SDK download anymore.   NuGet packages to install


Microsoft.CrmSdk.CoreAssemblies     v9.x
Microsoft.CrmSdk.XrmTooling.CoreAssembly   v9.x  

Then the code needs 2 new lines for it to work. Add a using System.Net; line, then as the new connections need TLS1.2 force this in the line ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; anywhere before you actually make the connection. So the code should now look like

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Tooling.Connector;
using System.ServiceModel;
using System.Net;
 
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var connectionString = "url=https://mycrmsystem; Username=myusername; Password=mypassword; authtype=Office365";
CrmServiceClient conn = new CrmServiceClient(connectionString);
using (OrganizationServiceProxy orgService = conn.OrganizationServiceProxy) {
     if (conn.IsReady) {
          // code to do stuff goes here
      }else{
         throw new invalidoperationexception(conn.LastCRMError);
      }
}

And you are done. 

Easy.

c#codev9 upgrade

  • Previous Old vs New Connection Methods in CRM2016/Dynamics 3656 years ago
  • Next Decoding JSON in C#6 years ago

Leave a Reply Cancel reply

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

Recent Posts

  • How to make your Powershell scripts more annoying
  • What motherboard is in my PC
  • A Dynamics365 plugin thought experiment
  • Registering a Dynamics365 plugin and I get an error
  • Going back in time with Dynamics365

Categories

  • Bootstrap
  • C#
  • CSS
  • Dot Net Core
  • Dynamics365
  • JavaScript
  • Powershell
  • T-SQL
  • Thoughts
  • VBScript
  • Visual Studio
  • Windows
  • Xamarin

Recent Comments

  • S0ftStuffB055 on Call a Dynamics365 workflow from JavaScript
  • Siva on Call a Dynamics365 workflow from JavaScript
  • TC Sharpe on Throw exceptions in Dynamics365 workflow plugins, or don’t
  • BigOwl on Throw exceptions in Dynamics365 workflow plugins, or don’t
  • CRMGod on Access a Dynamics365 embedded webresource

Archives

  • January 2021
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • June 2019
  • May 2019
  • February 2019
2025 Softstuff Consulting. Donna Theme powered by WordPress
  • Twitter