Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Consume External application API hosted on Azure in Dynamics V9 online plugin

(0) ShareShare
ReportReport
Posted on by 105

Hi All,

We are working on a requirement where we need to consume an API exposed by an application outside of Dynamics CRM, i.e. an external application. This API will be hosted on Azure.

We are on V9, Dynamics 365 online.

Can you please assist me with the steps I need to follow to consume this API(use GET and POST calls) through a Plugin/custom workflow. e.g. Get the access token, how to make the API calls etc.

Also, I intend to use Newtonsoft.json libraries for serialization and deserialization(so i guess I'll have to use ILMerge). But I'm looking for a sample code to achieve this.

Your help is greatly appreciated.

Thanks.

  • Toly Rogers Profile Picture
    Toly Rogers 105 on at
    RE: Consume External application API hosted on Azure in Dynamics V9 online plugin

    Hello,

    Thank you for the suggestion and for sharing the sample code with me. I was aware that Newtonsoft is officially not allowed, but I've been using it to interact with APIs called from console applications etc, so I've grown accustomed to using that.

    Do you have any sample code where Newtonsoft rest calls are made via plugins ?

    thanks

  • Toly Rogers Profile Picture
    Toly Rogers 105 on at
    RE: Consume External application API hosted on Azure in Dynamics V9 online plugin

    Thanks Mihir, appreciate your help with this.

  • Toly Rogers Profile Picture
    Toly Rogers 105 on at
    RE: Consume External application API hosted on Azure in Dynamics V9 online plugin

    Hi Ravi,

    Thank you for the detailed explanation. I can give this a try, but I've worked with Restclient using Newtonsoft while working with APIs hosted on IIS, but those calls were not initiated from Plugins/custom workflows.

    Is it possible to use newtonsoft at all? as I'm comfortable with that, and also wanted to understand if the approach you have listed will work with custom workflow activities as well?

    Thanks

  • Suggested answer
    Kalpavruksh D365 CoE Profile Picture
    Kalpavruksh D365 CoE 2,545 on at
    RE: Consume External application API hosted on Azure in Dynamics V9 online plugin

    Hi Toly,

    Please find the link for the official sample code.

    docs.microsoft.com/.../web-access-plugin

    github.com/.../WebAccessPlugin

    You can not use Newtonsoft.json because it's a third-party library and not supported inside the D365. Alternative to this use a DataContract serialization.

  • Suggested answer
    Mihir Maruti Kadam Profile Picture
    Mihir Maruti Kadam 702 on at
    RE: Consume External application API hosted on Azure in Dynamics V9 online plugin

    Use DataContact serialization.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Consume External application API hosted on Azure in Dynamics V9 online plugin

    Hi ,

    Assuming you are referring to REST API's. You can use WebRequest.Create to call the endpoint. You don't need to use Newtonsoft, to get the JSON response, you can create the classes with the JSON structure (see MyEmailResponse class below) and then use DataContractJsonSerializer for serialization of the response. Sample code below-

    dynamics365blogs.com/deserialize-json-data-using-datacontractjsonserializer

    ==================

    var httpWebRequest = (HttpWebRequest)WebRequest.Create(EmailEndPoint);

                   httpWebRequest.ContentType = "application/json";

                   httpWebRequest.Method = "POST";

                   // authentication

                   string encoded = Convert.ToBase64String(

                           Encoding.GetEncoding("ISO-8859-1").GetBytes(UserName + ":" + Password));

                   httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);

                   using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))

                   {

                       // make sure input is escaped

                       string json = "{ \"payload\": [{ \"address\": \"" + emailAddress + "\"}], " +

                                         " \"sourceOfTruth\": \"" + EmailSOT + "\"}";

                       streamWriter.Write(json);

                   }

                   // may need to handle exceptions

                   var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

                   using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))

                   {

                       var responseText = streamReader.ReadToEnd();

                       using (MemoryStream DeSerializememoryStream = new MemoryStream())

                       {

                           //initialize DataContractJsonSerializer object and pass Student class type to it

                           DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MyEmailResponse));

                           //user stream writer to write JSON string data to memory stream

                           StreamWriter writer = new StreamWriter(DeSerializememoryStream);

                           writer.Write(responseText);

                           writer.Flush();

                           DeSerializememoryStream.Position = 0;

                           //get the Desrialized data in object of type Student

                           MyEmailResponse objMyEmailResponse= (MyEmailResponse)serializer.ReadObject(DeSerializememoryStream);

                           if (objMyEmailResponse.payload[0].attributes.email_exists == "INVALID")

                           {

                               throw new InvalidPluginExecutionException("Error: Invalid email address. " + objMyEmailResponse.payload[0].attributes.message);

                           }

                       }

                   }

    ==================

    api1.png

    Hope this helps

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans