Using GOV.UK Notify in a Dynamics CRM Plugin Without ILMerge

Zoe Dawson
Oct 16, 2019 · 4 min read

With GOV.UK Notify’s SMS sending service becoming the next hip and trendy thing for government departments to use, it was only a matter of time before it had to be integrated with Dynamics CRM.

This blog post shows you how to create a Dynamics CRM plugin that can send text messages using this service, without using ILMerge, adding DLLs to the GAC or any other funny business.

It is important to try and avoid using ILMerge, as Microsoft has said it is unsupported. Adding DLLs to the GAC is not unsupported, but isn’t an option in online instances of Dynamics and can be difficult to maintain in on premise instances, requiring installation of every DLL you want to use into the GAC of every Dynamics server, and updating the DLLs on each of these servers when a new version of a DLL is released.

Dependencies

The first thing to tackle is the Gov.UK Notify DLL’s dependencies, as if we aren’t using ILMerge we won’t be able to add these DLLs into Dynamics for our plugin to use either. Gov.UK Notify has two dependencies: Newtonsoft.Json for JSON parsing and JWT for generating JWT authentication tokens.

Newtonsoft.Json

The Newtonsoft.Json library is used to create the JSON request to be sent to the Notify API. This can be replaced using the System.Web.Extensions JavaScript serializer. To use this, we have to create a class for every JSON object we want to parse; the ones you’ll need for the whole plugin are below:

The final step in replicating the Newtonsoft.Json DLL is writing a method to create our new Request object from the values we’re pulling out of CRM:

JWT

The second DLL we need to replace is JWT, which provides the JWT token generation functionality that the Notify API uses to authenticate. This is considerably more complicated than the Newtonsoft.Json replication; the first step was to Google what a JWT token is made from. Conveniently, the JWT website itself tells you exactly how a token is made (https://jwt.io/), so the next step is to recreate this in C#, using some of the JSON objects we created earlier to help us.

JWTs are comprised of three main parts; a header containing information on the type of token and algorithm used, a payload, which is a combination of the service ID (first half of your API key) and the current time in seconds, and finally the signature, which is the base64 encoded header and payload hashed using a secret, which in this case is the second half of your API key. The generate token function is shown below. I also added a little GetCurrentTimeInSeconds function to make it neater.

Now we have the JWT token and the JSON serialiser, it’s time to send a text. I used two functions for this, one to set up the client and one to send the request. These functions can be used to send one text, or in a for loop to send multiple, just put all of this logic in a class, create a new instance of the class in your plugin Execute method and call SendText().

Note that the “content” property in the dictionary should correspond to the name of the personalisation set in the template being used. For example, for the default templates, this would be “fullname” or “lastname”.

Conclusion

This blog post covered the reasons for using the GOV.UK Notify API over the DLL when sending SMS messages from Dynamics CRM plugins, how to replace the GOV.UK Notify DLL dependencies with your own code, and finally how to access and send a text using the API.

If it doesn’t work first time, don’t give up! It’s worth investing the time to make the code supported by Microsoft rather than using ILMerge to avoid the problem.

Join the Capgemini Microsoft team, open roles here.

Capgemini Microsoft Blog

Thought Leadership, Best Practices and Tips from Capgemini’s Microsoft Team

Capgemini Microsoft Blog

A place for Capgemini’s Microsoft team to share their experiences delivering cutting edge solutions for our clients using the latest Microsoft products. Follow us to keep up to date on trends, best practices, tips, revision guides and life in Capgemini’s Microsoft team.

Zoe Dawson

Written by

Software Engineer at Capgemini

Capgemini Microsoft Blog

A place for Capgemini’s Microsoft team to share their experiences delivering cutting edge solutions for our clients using the latest Microsoft products. Follow us to keep up to date on trends, best practices, tips, revision guides and life in Capgemini’s Microsoft team.