Sending Web Requests to a REST API from a Microsoft Dynamics 365 Plugin

Recently I was tasked with developing a plugin that would send information from Microsoft Dynamics 365 to a REST API. Originally, I had developed this plugin in an on-premise environment, which caused issues for when I eventually needed to move it to an Online instance. I will discuss more on this shortly.

image thumb 2 Sending Web Requests to a REST API from a Microsoft Dynamics 365 Plugin

After creating the base for the plugin, as shown in the screenshot above, and creating an empty method which would send data to an API, I needed to decide on a JSON Serializer. This proved to be the most important decision I would need to make. C# has a bunch of options for working with JSON Serialization with built-in examples like JavaScriptSerializer and DataContractJsonSerializer. Third party serializers also exist online which work well like Json.Net. Originally, I decided to use the JavascriptSerializer class, which was personally easier for me to use as I was more familiar with it. This seemed to work when fully developed, but the catch was that the plugin would have to be registered with Isolation Mode = None. This caused an issue when moving the plugin Online, as plugins must be registered in the Sandbox.

To work around this problem, I decided to change my web request code to use the DataContractJsonSerializer class instead. Doing this meant I would need to add DataContract and DataMember attributes to the object classes and slightly alter the data that is being passed into the request.

image thumb 3 Sending Web Requests to a REST API from a Microsoft Dynamics 365 Plugin

After making this change I could send web requests to a REST API from my plugin. The code above is the method I used to accomplish this, which made multiple Post requests and passed in multiple DataContract objects.

Let’s block ads! (Why?)

Magnetism Solutions Dynamics CRM Blog