9 Jun’17

Dynamics 365, EnterpriseHow to call a Web Service from Plugin in Dynamics CRM


In this blog, we will have a look on how a web service can be call from Plugin in Dynamics CRM.

Steps to be followed:

1)     Create an entity “Product Configuration” which consists of 2 fields

  • Key – Name of the Web Service
  • Value – Web Service URL

The basic purpose of this entity is to store the Web Service URL so that we don’t hard code the values in the code.

Web Service URL-

Where WorkOrder -> Controller Name and CreateWorkOrder -> Function Name

2)     Call the below function to call the Web Service. Retrieve Product Configuration function basically retrieves the record from CRM.

private void CallWebService(IPluginExecutionContext context, ITracingService tracer, IOrganizationService service)
            string licenseResposeJSON = string.Empty;
            ////// Retrieve Product Configuration details: URL 
            tracer.Trace("Retrieve Product Configuration details: URL");
            string value = string.Empty;
            value = this.RetrieveProductConfiguration(service);
            tracer.Trace("Downloading the target URI from Product Configuration: " + value);
            if (value != string.Empty)
                    using (WebClientEx client = new WebClientEx())
                        tracer.Trace("Call Web Service");

                        client.Timeout = 60000;
                        client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                        licenseResposeJSON = client.UploadString(value.ToString(), "1");
                   context.OutputParameters["WebServieCall"] = licenseResposeJSON;
                    tracer.Trace("Output Parameter is set: " + licenseResposeJSON);
                catch (WebException exception)
                    string str = string.Empty;
                    if (exception.Response != null)
                        using (StreamReader reader =
                            new StreamReader(exception.Response.GetResponseStream()))
                            str = reader.ReadToEnd();


                    if (exception.Status == WebExceptionStatus.Timeout)
                        throw new InvalidPluginExecutionException(
                            "The timeout elapsed while attempting to issue the request.", exception);

                    throw new InvalidPluginExecutionException(string.Format(CultureInfo.InvariantCulture, "A Web exception occurred while attempting to issue the request. {0}: {1}", exception.Message, str), exception);


public class WebClientEx : WebClient
            public int Timeout { get; set; }

            protected override WebRequest GetWebRequest(Uri address)
                var request = base.GetWebRequest(address);
                request.Timeout = Timeout;
                return request;

3)  Web Service which calls Create WorkOrder function is given below:

        public async Task CreateWorkOrder([FromBody]string value)
            CRM_DataOperations operations = new CRM_DataOperations();
            OperationResult result = await Task.Run(() => operations.CreateWorkOrder_Daily());
            return Request.CreateResponse(HttpStatusCode.Created, "Message: " + Enum.GetName(result.GetType(), result));

Hope you find this helpful! Thank you.


Written by

Krishna Bhanushali

Software Developer

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • Design Views in App Designer – D365

    11 April’ 2018

    Introduction: Create and edit public or system views by using the app designer Description: Dynamics 365 sales app des...

    Read more
  • Call Management in D365 Field Service

    10 April’ 2018

    Introduction: This blog provides a solution to requirement of Client for Call Management in D365 Field Service. Scenari...

    Read more
  • Fetch Hierarchical data for Product Category in Dynamics 365 Operations

    10 April’ 2018

    Introduction: In this blog article, we will see how we can fetch hierarchical data using X++. How to fetch? We will ta...

    Read more
  • PowerBI April 2018 Update: Combo Chart Line Formatting

    10 April’ 2018

    Introduction: PowerBI introduced the feature to format line styles and legends for line charts a while ago. However, th...

    Read more
  • Power BI April 2018 Update: Q&A Explorer

    10 April’ 2018

    Introduction: In this blog we will explore some of the new features added to Power BI. Power BI has upgraded its Q&...

    Read more