最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Execute workflow from JavaScript in CRM 2011 - Stack Overflow

programmeradmin0浏览0评论

I'm attempting to execute a workflow for records selected in a view, via a ribbon button. I have a working example using the 'legacy' services for CRM 4 patibility:

function invokeWorkflow(workflowId, entityId) {
    var request =
        '<soap:Envelope xmlns:soap="/"' +
        '               xmlns:xsi=""' +
        '               xmlns:xsd="">' +
           GenerateAuthenticationHeader() +
        '  <soap:Body>' +
        '    <Execute xmlns="">' +
        '      <Request xsi:type="ExecuteWorkflowRequest">' +
        '        <EntityId>' + entityId + '</EntityId>' +
        '        <WorkflowId>' + workflowId + '</WorkflowId>' +
        '      </Request>' +
        '    </Execute>' +
        '  </soap:Body>' +
        '</soap:Envelope>';

    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/MSCRMservices/2007/crmservice.asmx', false);

    xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
    xhr.setRequestHeader('SOAPAction', '');

    xhr.send(request);
}

However, I want to write this using CRM 2011 services to increase maintainability for future releases. Here is what I've tried so far, but this does not work - the return code of the call is HTTP 500 (Internal Server Error).

function invokeWorkflow(workflowId, entityId) {
    var request =
        '<soap:Envelope xmlns:soap="/"' +
        '               xmlns:xsi=""' +
        '               xmlns:xsd="">' +
        '  <soap:Body>' +
        '    <Execute xmlns="">' +
        '      <Request xsi:type="ExecuteWorkflowRequest">' +
        '        <EntityId>' + entityId + '</EntityId>' +
        '        <WorkflowId>' + workflowId + '</WorkflowId>' +
        '      </Request>' +
        '    </Execute>' +
        '  </soap:Body>' +
        '</soap:Envelope>';

    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/XRMServices/2011/Organization.svc/web', true);

    xhr.setRequestHeader('Accept', 'application/xml, text/xml, */*');
    xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
    xhr.setRequestHeader('SOAPAction', '');

    xhr.onreadystatechange = function () { alert(xhr.status); };
    xhr.send(request);
}

Does anyone know what's wrong with the second script? I've tried Googling this as best I can but every example I find which claims to be for CRM 2011 is in fact just using the CRM 4 patibility services (as in the first example). I've based the second example from a sample in the CRM 2011 SDK, although this doesn't include an example of the ExecuteWorkflowRequest object so it's best-guess only.

Thanks!

I'm attempting to execute a workflow for records selected in a view, via a ribbon button. I have a working example using the 'legacy' services for CRM 4 patibility:

function invokeWorkflow(workflowId, entityId) {
    var request =
        '<soap:Envelope xmlns:soap="http://schemas.xmlsoap/soap/envelope/"' +
        '               xmlns:xsi="http://www.w3/2001/XMLSchema-instance"' +
        '               xmlns:xsd="http://www.w3/2001/XMLSchema">' +
           GenerateAuthenticationHeader() +
        '  <soap:Body>' +
        '    <Execute xmlns="http://schemas.microsoft./crm/2007/WebServices">' +
        '      <Request xsi:type="ExecuteWorkflowRequest">' +
        '        <EntityId>' + entityId + '</EntityId>' +
        '        <WorkflowId>' + workflowId + '</WorkflowId>' +
        '      </Request>' +
        '    </Execute>' +
        '  </soap:Body>' +
        '</soap:Envelope>';

    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/MSCRMservices/2007/crmservice.asmx', false);

    xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
    xhr.setRequestHeader('SOAPAction', 'http://schemas.microsoft./crm/2007/WebServices/Execute');

    xhr.send(request);
}

However, I want to write this using CRM 2011 services to increase maintainability for future releases. Here is what I've tried so far, but this does not work - the return code of the call is HTTP 500 (Internal Server Error).

function invokeWorkflow(workflowId, entityId) {
    var request =
        '<soap:Envelope xmlns:soap="http://schemas.xmlsoap/soap/envelope/"' +
        '               xmlns:xsi="http://www.w3/2001/XMLSchema-instance"' +
        '               xmlns:xsd="http://www.w3/2001/XMLSchema">' +
        '  <soap:Body>' +
        '    <Execute xmlns="http://schemas.microsoft./xrm/2011/Contracts/Services">' +
        '      <Request xsi:type="ExecuteWorkflowRequest">' +
        '        <EntityId>' + entityId + '</EntityId>' +
        '        <WorkflowId>' + workflowId + '</WorkflowId>' +
        '      </Request>' +
        '    </Execute>' +
        '  </soap:Body>' +
        '</soap:Envelope>';

    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/XRMServices/2011/Organization.svc/web', true);

    xhr.setRequestHeader('Accept', 'application/xml, text/xml, */*');
    xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
    xhr.setRequestHeader('SOAPAction', 'http://schemas.microsoft./xrm/2011/Contracts/Services/IOrganizationService/Execute');

    xhr.onreadystatechange = function () { alert(xhr.status); };
    xhr.send(request);
}

Does anyone know what's wrong with the second script? I've tried Googling this as best I can but every example I find which claims to be for CRM 2011 is in fact just using the CRM 4 patibility services (as in the first example). I've based the second example from a sample in the CRM 2011 SDK, although this doesn't include an example of the ExecuteWorkflowRequest object so it's best-guess only.

Thanks!

Share Improve this question asked Jul 18, 2012 at 16:18 AlecAlec 9661 gold badge11 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

There is an application named SOAPLogger in the CRM sdk folder \samplecode\cs\client\soaplogger that generates requests in javascript for specific actions.

Below, you can find the http request for "ExecuteWorkflow" (just change the value for EntityIdValue and WorkflowIdValue).

<s:Envelope xmlns:s="http://schemas.xmlsoap/soap/envelope/">
  <s:Body>
    <Execute xmlns="http://schemas.microsoft./xrm/2011/Contracts/Services" xmlns:i="http://www.w3/2001/XMLSchema-instance">
      <request i:type="b:ExecuteWorkflowRequest" xmlns:a="http://schemas.microsoft./xrm/2011/Contracts" xmlns:b="http://schemas.microsoft./crm/2011/Contracts">
        <a:Parameters xmlns:c="http://schemas.datacontract/2004/07/System.Collections.Generic">
          <a:KeyValuePairOfstringanyType>
            <c:key>EntityId</c:key>
            <c:value i:type="d:guid" xmlns:d="http://schemas.microsoft./2003/10/Serialization/">EntityIdValue</c:value>
          </a:KeyValuePairOfstringanyType>
          <a:KeyValuePairOfstringanyType>
            <c:key>WorkflowId</c:key>
            <c:value i:type="d:guid" xmlns:d="http://schemas.microsoft./2003/10/Serialization/">WorkflowIdValue</c:value>
          </a:KeyValuePairOfstringanyType>
        </a:Parameters>
        <a:RequestId i:nil="true" />
        <a:RequestName>ExecuteWorkflow</a:RequestName>
      </request>
    </Execute>
  </s:Body>
</s:Envelope>

The construction of XMLHttpRequest is corect, so try changing the soapEnvelope.

发布评论

评论列表(0)

  1. 暂无评论