使用javascript CRM 2011 RU 12调用外部Web服务(汇总12)

时间:2015-04-23 06:17:24

标签: javascript web-services soap dynamics-crm-2011

直到最近,我们才在我们的Dynamics CRM 2011应用程序中使用RU 11,但最近开始了一个项目,最终转向CRM 2015.我们升级到RU 12,这是事情变得有趣的地方!

我已经设法解决了之前存在的许多编码问题,但是在我的生活中无法解决我们访问外部Web服务的问题,一路上传递参数以及检索数据。

起初我遇到了“CreateXmlHttp()”函数的问题,但在网上找到了一个很好的答案,表明以下函数会有所帮助:

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;         
}

这很好,正如预期的那样,我现在可以连接到服务了。

但是,我没有检索到任何数据,这就是将我绑起来的问题。

作为背景,以下是我的代码,用于在将xml传递给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 the 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 the 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 the 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 all parameters have been entered - it is not possible to generate a key without all parameters');
}

在RU 12之前,代码工作正常,并且从Web服务检索了最后一个变量“baseKey”。

然而,整个过程现在完全正常,因为它似乎连接正常,但只是没有从服务中检索“baseKey”。

我认为这个问题可能与弃用的端点或模式有关,甚至只是返回并显示值的弃用代码(即var xmlDoc = Request.responseXML;)但我不知道在哪里解决。

如果有人有任何想法或能指出我正确的方向,那将是一个很大的帮助。

1 个答案:

答案 0 :(得分:0)

我们有类似要求调用外部Web-service来检索联系人的latest contract并在Contact Form.上显示

我们有自己的latest contract定义,这是在webserivce中实现的。所以我必须在Contact Form Load上调用该Web服务并提取数据。我找不到答案。所以我发布了我的解决方法。

我写了plugin,在Contact的检索上执行。从插件中我调用了Webservice并更新了联系人字段。

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 as JSON and write to 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 and 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);