Call external Web Service using javascript CRM 2011 RU 12 (Rollup 12) -
until we'd been using ru 11 within our dynamics crm 2011 application have begun project move crm 2015. we've upgraded ru 12 , things fun!
i've managed sort out lot of coding issues present cannot life of me solve our issue of accessing external web service, passing parameters along way , retrieving data back.
at first had trouble "createxmlhttp()" function found great answer online indicating following function help:
function createxmlhttp() { var ref = null; if (window.xmlhttprequest) { ref = new xmlhttprequest(); } else if (window.activexobject) { // older ie ref = new activexobject("msxml2.xmlhttp.3.0"); } return ref; }
this worked fine , expected, have appeared connect service.
however, not retrieving data , issue tying me in knots.
as background, following code declare few integer variables before posting xml through webservice.
parseint(lnglicensnr); parseint(lngnrofusers); parseint(lngannualworkingtime); parseint(lngincludedhours); parseint(producttype); if (licencetype == 100000000) { var licensetype = "ltunlimitedmode"; lngincludedhours = 1000000; } if (licencetype == 100000001) { var licensetype = "lthourlymode"; } if (lnglicensnr != null && lngnrofusers != null && lngannualworkingtime != null && lngincludedhours != null && producttype != null && licensetype != null) { var errmsg = ""; var errcount = 0; var xml = "<soap:envelope xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"; xml += "<soap:body>"; xml += "<getbaseinstallationkey xmlns=\"http://www.keyservice.com/\">"; xml += "<licensnr>" + lnglicensnr + "</licensnr>"; xml += "<nrofusers>" + lngnrofusers + "</nrofusers>"; xml += "<annualworkingtime>" + lngannualworkingtime + "</annualworkingtime>"; xml += "<includedhours>" + lngincludedhours + "</includedhours>"; xml += "<producttype>" + baseproduct + "</producttype>"; xml += "<licensetype>" + licensetype + "</licensetype>"; xml += "</getbaseinstallationkey>"; xml += "</soap:body>"; xml += "</soap:envelope>"; try { request = createxmlhttp(); } catch (ex) { alert("an error occured while accessing webservice.\n\n1. " + ex.name + " \n " + ex.message + "\n\n"); errcount = 1; } if (errcount == 0) { try { request.open("post", "http://web01:8080/keywebservice/keywebservice.asmx", false); } catch (ex) { alert("an error occured while accessing webservice.\n\n2. " + ex.name + " \n " + ex.message + "\n\n"); errcount = 1; } } if (errcount == 0) { try { request.setrequestheader("soapaction", "http://www.keyservice.com/getkey"); request.setrequestheader("host", "web01:8080"); request.setrequestheader("content-type", "text/xml; charset=utf-8"); request.setrequestheader("content-length", xml.length); request.send(xml); var xmldoc = request.responsexml; } catch (ex) { alert("an error occured while accessing webservice.\n\n3. " + ex.name + " \n " + ex.message + "\n\n"); errcount = 1; } } if (errcount == 0 || request.responsexml != null) { basekey = request.responsexml.text; } else { basekey = "no key generated"; } } if (lnglicensnr == null || lngnrofusers == null || lngannualworkingtime == null || lngincludedhours == null || producttype == null || licensetype == null) { alert('not parameters have been entered - not possible generate key without parameters'); }
prior ru 12, code worked absolutely fine , last variable "basekey" retrieved web service.
however, whole process works fine in seems connect ok not retrieve "basekey" service.
i believe issue may related deprecated end points or schemas or deprecated code returns , displays value (i.e. var xmldoc = request.responsexml;) i'm not sure resolve.
if has ideas or can point me in right direction big help.
we had similar requirement call external web-service
retrieve latest contract
of contact , show on contact form.
we had our own definition of latest contract
implement in webserivce. have call webservice on contact form load
, pull data. not find answer. posting workaround this.
i had written plugin
executes on retrieve of contact
. plugin had call webservice
, updates contact fields.
var javascriptserializer = new javascriptserializer(); javascriptserializer.maxjsonlength = 104857600; //200 mb unicode stringbuilder uri = new stringbuilder(); uri.append(crmrestwebserviceurl).append(webservicemethod); var request = (httpwebrequest)webrequest.create(uri.tostring()); request.method = "post"; request.accept = "application/json"; request.contenttype = "application/json; charset=utf-8"; //serialize request object json , write request body if (latestmembershiprequest != null) { var stringbuilder = new stringbuilder(); javascriptserializer.serialize(latestmembershiprequest, stringbuilder); var requestbody = stringbuilder.tostring(); request.contentlength = requestbody.length; var streamwriter = new streamwriter(request.getrequeststream(), system.text.encoding.ascii); streamwriter.write(requestbody); streamwriter.close(); } var response = request.getresponse(); //read json response stream , deserialize var streamreader = new system.io.streamreader(response.getresponsestream()); var responsecontent = streamreader.readtoend().trim(); latestmembershipresponse latestmembershipresponse = javascriptserializer.deserialize<latestmembershipresponse>(responsecontent); contacttoupdate["gr_membershiptitle"] = latestmembership.membershiptitle; contacttoupdate["gr_membershipstartdate"] = convert.todatetime(latestmembership.membershipstartdate); contacttoupdate["gr_membershipenddate"] = convert.todatetime(latestmembership.membershipenddate); contacttoupdate["gr_membershipstatus"] = latestmembership.membershipstatus; service.update(contacttoupdate);