Friday, July 24, 2015

External Service call (Web access) from a CRM plug-in - CRM Dynamics 2015 / 2013 /2011 (Delete Json Call Message)

Call external web service from CRM Dynamics. Delete Json call
 


            //Json Data
           var pairs = new NameValueCollection()
            {
                 { "bypassCRM", "true"},
                 { "FirstName", "Vikram"},
                 { "LastName", "Tupe"},
            };

            byte[] response = null;

            using (WebClient client = new WebClient())
            {
       //Attach Headers if any.... (Below Authorization is custom header required in my case)
                client.Headers["Authorization"] = "AuthToken";
                response = client.UploadValues(url, "DELETE", pairs);
            }

            var text = System.Text.Encoding.UTF8.GetString(response);

            /* Above is equivalent to following web request/ json */
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "DELETE";
            httpWebRequest.Headers.Add("Authorization:AuthToken");
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
           {
               //Json Data
                string json = "{\"FirstName\":\"" + "Vikram" + "\"," +
                        "\"LastName\":\"" + "Test" + "\"," +
                        "\"bypassCRM\":\"true\"}";
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
               {
                    text = streamReader.ReadToEnd();
                }
            }

 

 

 

 

No comments: