Consume web service from plugin


Many times, during our MS CRM development we needs to interact with other application, we called it ‘integration’, we have multiple ways for it

  1. we can write stand alone application for it, which will interact both the application
  2. consume web service or open same for outside world etc …

First of all, make sure that provided sample SOAP is working fins with provided end points, so it will save your lots of efforts, you will get plenty of applications over the net. I used used one of it, you may use it by downloading it from here. This application looks like below,

Add your ‘WSDL’ url and click ok button, ref bow screen shot


Now, now you may, test your SOAP, REST etc, ref below screenshot


Sample SOAP request


Now we are ready for next part,

By CRM perspective, we can consume web service in plugin as well, I will give a walk though of it here …

Steps for that are

  1. write a plugin
  2. create string of SOAP, ref below e.g.
  3. here
  4. xmlns1 = “http://schemas.****.com/eai/DocProdServices/0.9.20151021/GnSynch”;
  5. xmlns1 =”http://schemas.****.com/eai/DPServices/0.9.20151021/DTypes”;
  6. private string GetRequestString(XDocument QuoteXML, Guid UserID, string xmlns1, string xmlns0)
     Guid requestId = Guid.NewGuid();
     string timeStamp = System.DateTime.Now.ToString("O");
     Entity soapConfiguartion = getSoapConfigurationDetails();
     string request =
     @"<soapenv:Envelope xmlns:soapenv="""" xmlns:gen='" + xmlns1 + "' xmlns:dps='" + xmlns0 + "'>" +
     "<soapenv:Header/>" +
     "<soapenv:Body>" +
     "<gen:SynchRequest>" +
     @"<gen:XPRequest><![CDATA[<?xml version=""1.0"" encoding=""UTF-8""?>" +
    @"<XPR_QUOTE_QUOTEDATA xmlns:xsi="""" xsi:noNamespaceSchemaLocation=""p:\xpression\*******data\schema\XPR_**_CRM.xsd"">" +
     QuoteXML.ToString() +
     "</XPR_QUOTE_QUOTEDATA>]]></gen:XPRequest>" +
     "<dps:Manifest>" +
     "<dps:RequestId>" + requestId.ToString() + "</dps:RequestId>" +
     "<dps:Company>HSBGS</dps:Company>" +
     "<dps:Unit>QUOTE</dps:Unit>" +
     "<dps:SourceSystem>CRM</dps:SourceSystem>" +
     "<dps:UserId>" + UserID.ToString() + "</dps:UserId>" +
     "<dps:RequestTimestamp>" + timeStamp + "</dps:RequestTimestamp>" +
     "</dps:Manifest>" +
     "</gen:SynchRequest>" +
     "</soapenv:Body>" +
     return request;
  7. generate request
    Here requestStr stands for above generated request string and Url is "http://***.dev.******.com/EAI_Service/DPServices/0.9.20151021/DPServices.svc";
  8. and 'action' is the function which will be called in my case 'G***Synch'
  9. private HttpWebRequest BizTalkWebRequest(string requestStr, string url, string action)
     System.Net.WebRequest.DefaultWebProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
     webRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;
     // Specify properties for request object 
     webRequest.Method = "POST";
     webRequest.Timeout = System.Threading.Timeout.Infinite;
     webRequest.Accept = "text/xml";
     webRequest.ContentType = "text/xml;charset=\"utf-8\"";
     // Specify the Create method to be called in this request 
     webRequest.Headers.Add("SOAPAction", action);
     // Write Request XML on the request object 
     byte[] byteArray = Encoding.UTF8.GetBytes(requestStr);
     webRequest.ContentLength = byteArray.Length;
     using (Stream Stream = webRequest.GetRequestStream())
     Stream.Write(byteArray, 0, byteArray.Length);
     return webRequest;
  10. collect the response of web-service
  11. private void ProcessSoapResponce(Entity QuoteEntity, XNamespace xmlns1, XNamespace xmlns0, HttpWebRequest webRequest)
     IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
     string soapResult;
     using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
     using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
     soapResult = rd.ReadToEnd();
     XDocument xDoc = XDocument.Parse(soapResult);
     //get desied data from SOAP request
     var statusCode = xDoc.Descendants((XNamespace)"" + "Body").
     Descendants((XNamespace)xmlns1 + "StatusCode").FirstOrDefault();
     string fileName = "Some Name" + "_" + System.DateTime.Now.ToString("dd MMMM yyy");
     string Filepath = fileName + ".pdf"; 
     //for successfule SOAP responce
     if (statusCode.Value == "0")
     Guid guid = Guid.NewGuid();
     //get desied data from SOAP request
     var FileByteStream = xDoc.Descendants((XNamespace)"" + "Body").
     Descendants((XNamespace)xmlns0 + "Document").
     Descendants((XNamespace)xmlns0 + "FileByteStream").FirstOrDefault();
     //convert received string into byte base 64
     var fileData = Convert.FromBase64String(FileByteStream.Value);
     PDFattachment("quote", quoteId, fileName + ".pdf", fileData); 
     //for unsuccessfule SOAP responce
     else if (statusCode.Value == "1")
     //get desied data from SOAP request
     var faultstring = xDoc.Descendants((XNamespace)"" + "Body").
     Descendants((XNamespace)xmlns0 + "Fault").
     Descendants((XNamespace)xmlns0 + "faultstring").
     if (faultstring != null)
     //convert to base 64
     var fileData = Convert.FromBase64String(faultstring.Value);
     AttachFileinNote("hsb_triggerquotexml", quoteXmlId, xDoc.ToString(), "Error Responce", "Received SOAP responce ";

That’s it.


One thought on “Consume web service from plugin

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s