如何使用 SOAP 调用设置电子邮件对象的状态?

How do you set the state and status of an email object using a SOAP call?

我真的很难解决这个问题。我在网上搜索并找到了一些关于如何设置对象的状态和状态的示例,但它们似乎都不适合我。

有没有人提供一些示例,说明 soap 信封的外观以及 Msxml2.XMLHTTP 对象的属性对于同步调用 Web 服务应该是什么?

提前致谢, 灰色

编辑

好的......所以出于一个无法解释的原因,我对此投了反对票,但没有答案,所以我想我会描述我目前得到的,以及我得到的。

这是主要功能(从我发现的其中一个例子中得到了很大程度的提升:

 function setStateCodeForEntity(entityName, entityId, stateOptionsetValue, statusOptionsetValue) {


    var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    request += "<s:Body>";
    request += "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    request += "<request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    request += "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>EntityMoniker</c:key>";
    request += "<c:value i:type=\"a:EntityReference\">";
    request += "<a:Id>" + entityId + "</a:Id>";
    request += "<a:LogicalName>" + entityName + "</a:LogicalName>";
    request += "<a:Name i:nil=\"true\" />";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>State</c:key>";
    request += "<c:value i:type=\"a:OptionSetValue\">";
    request += "<a:Value>"+stateOptionsetValue+"</a:Value>";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>Status</c:key>";
    request += "<c:value i:type=\"a:OptionSetValue\">";
    request += "<a:Value>"+statusOptionsetValue+"</a:Value>";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "</a:Parameters>";
    request += "<a:RequestId i:nil=\"true\" />";
    request += "<a:RequestName>SetState</a:RequestName>";
    request += "</request>";
    request += "</Execute>";
    request += "</s:Body>";
    request += "</s:Envelope>";

var ret = executeSoapRequest("execute", request);

}

executeSoapRequest 函数如下所示:

function executeSoapRequest(action, xml) {
var actionUrl = "http://schemas.microsoft.com/crm/2007/WebServices/" + action;

var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", actionUrl);
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);

return xmlHttpRequest.responseXML;

}

并且我已经尝试调用顶部描述的方法(请注意,newGuid 被分配到其他地方,并且包含我希望为其设置 state/status 的新电子邮件记录的 Guid) :

setStateCodeForEntity("email", newGuid, 1, 2);

setStateCodeForEntity("email", newGuid, "Completed", 2);

我得到的是服务器 500 错误,响应如下:

"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"/>\r\n<title>500 - Internal server error.</title>\r\n<style type=\"text/css\">\r\n<!--\r\nbody{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}\r\nfieldset{padding:0 15px 10px 15px;} \r\nh1{font-size:2.4em;margin:0;color:#FFF;}\r\nh2{font-size:1.7em;margin:0;color:#CC0000;} \r\nh3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} \r\n#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:\"trebuchet MS\", Verdana, sans-serif;color:#FFF;\r\nbackground-color:#555555;}\r\n#content{margin:0 0 0 2%;position:relative;}\r\n.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}\r\n-->\r\n</style>\r\n</head>\r\n<body>\r\n<div id=\"header\"><h1>Server Error</h1></div>\r\n<div id=\"content\">\r\n <div class=\"content-container\"><fieldset>\r\n  <h2>500 - Internal server error.</h2>\r\n  <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>\r\n </fieldset></div>\r\n</div>\r\n</body>\r\n</html>\r\n<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><soap:Fault><faultcode>Server</faultcode><faultstring>Object reference not set to an instance of an object.</faultstring><detail><error xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><displaytitle /><displaytext /><description>Object reference not set to an instance of an object.</description><file>Not available</file><line>Not available</line><details>Not available</details><requesturl>http://[SERVERNAME]/mscrmservices/2007/CrmService.asmx</requesturl><pathAndQuery>/mscrmservices/2007/CrmService.asmx</pathAndQuery><source>SOAP</source><stacktrace /></error></detail></soap:Fault></soap:Body></soap:Envelope>"

然而...

我刚刚注意到,当我使用确实成功的 executeSoapRequest 方法时,我得到了一个非常相似的 responseText。当我如上所述拨打电话时,电子邮件实体的状态没有改变。

我几乎肯定在做一些愚蠢的事情,但我不知道是什么。此时任何建设性的帮助都将是救命稻草。

灰色

好的...为了完整起见,这里是答案:

主要函数如下所示:

function setStateCodeForEntity(entityName, entityId, stateOptionsetValue, statusOptionsetValue) {

    var request = [
        "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>",
        GenerateAuthenticationHeader(),
        "<soap:Body>",
        "<Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>", 
        "<Request xsi:type='SetStateDynamicEntityRequest'>",
        "<Entity>",
        "<Name xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>", entityName, "</Name>",
        "<Id xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>", entityId, "</Id>",
        "</Entity>",
        "<State>", stateOptionsetValue, "</State>",
        "<Status>", statusOptionsetValue, "</Status>",
        "</Request>",
        "</Execute>",
        "</soap:Body>",
        "</soap:Envelope>"
        ].join("");

        var ret = executeSoapRequest("Execute", request);
 }

注意请求类型是'SetStateDynamicEntityRequest',这似乎是我最初尝试的主要问题,而且这个版本比我最初使用的要简单得多。

方法被

调用
setStateCodeForEntity("email", newGuid, 'Completed', 2);

一位推荐 Crm 服务工具包的同事给了我答案 (http://crmtoolkit.codeplex.com/) and the code was pretty much lifted from there. Further information at http://danielcai.blogspot.co.uk/2010/07/crm-web-service-toolkit-for-javascript.html