I want to Execute workflow with SOAP. I've googled all around this and tried different ways. But none of my code works. For example, two most final variants are: for 2011 Service
function RunWorkflow() {
var url = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/Organization.svc/web";
var requestMain = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"> <s:Body>";
requestMain += " <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
requestMain += " <request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
requestMain += " <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>EntityId</c:key>";
requestMain += " <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">B8373961-D084-E611-8763-005056810E95</c:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>WorkflowId</c:key>";
requestMain += " <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">191CDD5A-3EBD-40FD-A685-B79D99B32831</c:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " </a:Parameters>";
requestMain += " <a:RequestId i:nil=\"true\" />";
requestMain += " <a:RequestName>ExecuteWorkflow</a:RequestName>";
requestMain += " </request>";
requestMain += " </Execute>";
requestMain += " </s:Body>";
requestMain += "</s:Envelope>";
var req = new XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
req.onreadystatechange = function () { assignResponse(req); };
req.send(requestMain);
function assignResponse(req) {
if (req.readyState == 4)
if (req.status == 200)
alert('successfully executed the workflow');
}
}
for 2007 Service
function RunWorkflowSeven() {
var url = Xrm.Page.context.getServerUrl() + "/MSCRMservices/2007/CrmServiceWsdl.aspx";
var soapBody = "<s:Envelope><soap:Body>" +
" <Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
" <Request xsi:type=\'ExecuteWorkflowRequest\'>" +
" <EntityId>B8373961-D084-E611-8763-005056810E95</EntityId>" +
" <WorkflowId>191CDD5A-3EBD-40FD-A685-B79D99B32831</WorkflowId>" +
" </Request>" + " </Execute>" + "</soap:Body>";
var soapXml = "<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() + soapBody + "</soap:Envelope>";
var req = new XMLHttpRequest();
req.open("POST", url, false);
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
req.onreadystatechange = function () { assignResponse(req); };
req.send(soapXml);
function assignResponse(req) {
if (req.readyState == 4)
if (req.status == 200)
alert('successfully executed the workflow');
}
}
The first gives me an error '400: Bad Request',
Other variant of 2011 (request to '/XRMServices/2011/Organization.svc/' instead of '/XRMServices/2011/Organization.svc/web' gives 'Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'),
The second shows no errors in console but it doesn't run workflow.
Sorry for such a big code but I really don't know where the problem lies.
UPDATE 1: Fiddler shows 3 requests with following status:
I believe in first request you missed one node. Try to replace
var requestMain = " <s:Body>";
with
var requestMain = "<s:Envelope><s:Body>";