locked
How to call an Azure Web API (2.2) - from Dynamics Custom WorkFlow Activity of Plugin c# RRS feed

  • Question

  • Hello,

    I am specifically trying to call a web api, that has a FromBody.

    However, I am trying to do it without having to use libraries (or nuget packages) that I cannot install on CRM of CRM Online.

    It seems JSON newtonsoft is big... or I guess I could use my own custom formatter...

    But I would love to see if someone has an example for posting to web API, versus an ASMX

    Thanks

    Sunday, April 26, 2015 4:45 PM

Answers

  • Wow I had this awesome thing typed up... and it wouldn't do it then wanted me to verify and then that messed up and I lost it all... Wow..

    Anyway

    [SOLVED]

    • Works from Plugin or Workflow activity
    • Use the Query string to pass things (not the body) or else you have to compress it and you can't use the libraries to DO that in the Sandbox... You CAN do that if you or on premise and do NOT isolate.. Then heck I did everything I wanted but for online this was it.
    • You do NOT have to use WebClient, use HttpClient (follow the code below)
    • WebApi 2.2 is what I used
    • My signature for Webapi
    • You do have to clean up the response.. as the string you get back is JSON.. and you cannot use the JSON desieralizer...
    • Easy fix... just do a replacement of the \ and " to a " " space.. then Trim the result.. bingo a clean string response...

    [HttpGet]

    Public string MyMethod(string value1, string value2, string value3)

    • This will parse the query string so USE these are your parameters in your query string or the API will NOT work. ?value1=ddd&&value2=ddd&&value3=aaa
    • of course make up your own string names
    • IIS and Apache etc work differently so the AMOUNT of data you can send will differ per platform, per version of platform AND usually you can configure it.. IIS used to be like 2048 by default.. anything beyond that (as a total URL incoming) was truncated.. Not sure now. I know it was configurable

    I got it working.. all my points are gone... but it's possible

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Activities;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Workflow;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm;
    using System.Net.Http;
    using System.IO;
    using System.IO.Compression;
    using System.Net.Http.Headers;
    using System.Runtime.Serialization.Json;
    using System.ServiceModel;
    using System.Net;

    try
    {
        string result = string.Empty;
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(webapihost);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
            client.Timeout = new TimeSpan(0, 0, 30); ;
            client.MaxResponseContentBufferSize = 2000000;

            using (HttpResponseMessage response = client.GetAsync(webapihost + apimethod + querystring).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    result = Decompress(response);
                    tracingService.Trace("Call Data Value [{0}]", result.ToString());
                }
            }
        }

    }

    catch (Exception simple)
    {
        throw new InvalidPluginExecutionException("Error with simple HTTPClient x-www-form-urlencoded" + simple.ToString());
    }

    private string Decompress(HttpResponseMessage response)
    {
        try
        {
            switch (response.Content.Headers.ContentEncoding.ToString().ToLower())
            {
                case "gzip":
                    {
                        return DecompressGzip(response.Content.ReadAsStreamAsync().Result);
                    }
                case "deflate":
                    {
                        return Inflate(response.Content.ReadAsStreamAsync().Result);
                    }
                default:
                    {
                        return response.Content.ReadAsStringAsync().Result;
                    }
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error decompressing: " + ex.ToString());
        }
    }

    private string DecompressGzip(Stream datastream)
    {
        try
        {
            //Create a new stream
            //decompress the original
            //get back the string (JSON)

            if (datastream != null)
            {
                Stream decompressedStream = null;

                decompressedStream = new GZipStream(datastream, CompressionMode.Decompress, true);
               
                var streamReader = new StreamReader(decompressedStream);
                string stringdata = streamReader.ReadToEnd();
               
                decompressedStream.Dispose();

                return stringdata;
            }
            else
            {
                return string.Empty;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Gzip error: " + ex.ToString());
        }
    }
    private string Inflate(Stream datastream)
    {
        try
        {
            if (datastream != null)
            {
                Stream decompressedStream = null;

                decompressedStream = new DeflateStream(datastream, CompressionMode.Decompress, true);

                var streamReader = new StreamReader(decompressedStream);

                string stringdata = streamReader.ReadToEnd();

                decompressedStream.Dispose();

                return stringdata;
            }
            else
            {
                return string.Empty;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Inflate error: " + ex.ToString());
        }
    }


    • Edited by Anokneemous Saturday, May 2, 2015 4:30 PM typo in a value
    • Marked as answer by Anokneemous Monday, May 4, 2015 3:24 PM
    Saturday, May 2, 2015 4:29 PM

All replies

  • This might not be the answer you are looking for, but you CAN use 3rd party libraries and assemblies with CRM Online, but you have to merge them into your final Plugin assembly with ILMerge.

    https://social.microsoft.com/Forums/en-US/79e2634d-555f-4a22-8a9a-7ec5de43a9fd/thirdparty-dll-reference-with-crm-online?forum=crmdevelopment

    It's a little more work to setup for your builds, but it does allow you to use other libraries and nuget packages.

    Good luck!

    Tuesday, April 28, 2015 12:23 PM
  • Wow I had this awesome thing typed up... and it wouldn't do it then wanted me to verify and then that messed up and I lost it all... Wow..

    Anyway

    [SOLVED]

    • Works from Plugin or Workflow activity
    • Use the Query string to pass things (not the body) or else you have to compress it and you can't use the libraries to DO that in the Sandbox... You CAN do that if you or on premise and do NOT isolate.. Then heck I did everything I wanted but for online this was it.
    • You do NOT have to use WebClient, use HttpClient (follow the code below)
    • WebApi 2.2 is what I used
    • My signature for Webapi
    • You do have to clean up the response.. as the string you get back is JSON.. and you cannot use the JSON desieralizer...
    • Easy fix... just do a replacement of the \ and " to a " " space.. then Trim the result.. bingo a clean string response...

    [HttpGet]

    Public string MyMethod(string value1, string value2, string value3)

    • This will parse the query string so USE these are your parameters in your query string or the API will NOT work. ?value1=ddd&&value2=ddd&&value3=aaa
    • of course make up your own string names
    • IIS and Apache etc work differently so the AMOUNT of data you can send will differ per platform, per version of platform AND usually you can configure it.. IIS used to be like 2048 by default.. anything beyond that (as a total URL incoming) was truncated.. Not sure now. I know it was configurable

    I got it working.. all my points are gone... but it's possible

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Activities;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Workflow;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm;
    using System.Net.Http;
    using System.IO;
    using System.IO.Compression;
    using System.Net.Http.Headers;
    using System.Runtime.Serialization.Json;
    using System.ServiceModel;
    using System.Net;

    try
    {
        string result = string.Empty;
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(webapihost);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
            client.Timeout = new TimeSpan(0, 0, 30); ;
            client.MaxResponseContentBufferSize = 2000000;

            using (HttpResponseMessage response = client.GetAsync(webapihost + apimethod + querystring).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    result = Decompress(response);
                    tracingService.Trace("Call Data Value [{0}]", result.ToString());
                }
            }
        }

    }

    catch (Exception simple)
    {
        throw new InvalidPluginExecutionException("Error with simple HTTPClient x-www-form-urlencoded" + simple.ToString());
    }

    private string Decompress(HttpResponseMessage response)
    {
        try
        {
            switch (response.Content.Headers.ContentEncoding.ToString().ToLower())
            {
                case "gzip":
                    {
                        return DecompressGzip(response.Content.ReadAsStreamAsync().Result);
                    }
                case "deflate":
                    {
                        return Inflate(response.Content.ReadAsStreamAsync().Result);
                    }
                default:
                    {
                        return response.Content.ReadAsStringAsync().Result;
                    }
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error decompressing: " + ex.ToString());
        }
    }

    private string DecompressGzip(Stream datastream)
    {
        try
        {
            //Create a new stream
            //decompress the original
            //get back the string (JSON)

            if (datastream != null)
            {
                Stream decompressedStream = null;

                decompressedStream = new GZipStream(datastream, CompressionMode.Decompress, true);
               
                var streamReader = new StreamReader(decompressedStream);
                string stringdata = streamReader.ReadToEnd();
               
                decompressedStream.Dispose();

                return stringdata;
            }
            else
            {
                return string.Empty;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Gzip error: " + ex.ToString());
        }
    }
    private string Inflate(Stream datastream)
    {
        try
        {
            if (datastream != null)
            {
                Stream decompressedStream = null;

                decompressedStream = new DeflateStream(datastream, CompressionMode.Decompress, true);

                var streamReader = new StreamReader(decompressedStream);

                string stringdata = streamReader.ReadToEnd();

                decompressedStream.Dispose();

                return stringdata;
            }
            else
            {
                return string.Empty;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Inflate error: " + ex.ToString());
        }
    }


    • Edited by Anokneemous Saturday, May 2, 2015 4:30 PM typo in a value
    • Marked as answer by Anokneemous Monday, May 4, 2015 3:24 PM
    Saturday, May 2, 2015 4:29 PM