繁体   English   中英

Dynamics Crm从外部来源调用工作流程

[英]Dynamics Crm Invoking Workflow from external source

场景:

我想从CRM Dynamics上下文外部的网页调用已定义的工作流程或自定义操作。 (假设MS CRM 2011-2013-2015-2016和365)

我的解决方案:

我的想法是在可从Web访问的CRM上下文中定义一种控制器页面,并在该页面中执行其余调用(通过javascript)。
该页面将能够读取输入参数并执行正确的rest调用。

是否有意义? 您能否建议一个更好的实施方案?

提前致谢!

如果有足够的资源,则可以使用以下方法设置服务,然后对其进行ajax。

private static void ExecuteWorkflow(Guid workflowId, Guid entityId)
    {
        try
        {
            string url = ConfigurationManager.ConnectionStrings["crm"].ConnectionString;
            ClientCredentials cc = new ClientCredentials();
            cc.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            OrganizationServiceProxy _service = new OrganizationServiceProxy(new Uri(url), null, cc, null);

            ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()
            {
                WorkflowId = workflowId,
                EntityId = entityId
            };

            ExecuteWorkflowResponse r = (ExecuteWorkflowResponse)_service.Execute(request);
            _service.Dispose();
        }
        catch (Exception ex)
        {
            //Handle Exception
        }
    }

如果您无法在CRM服务器所在的域中使用该服务,则应该可以模拟。

cc.Windows.ClientCredential.Domain = "DOMAIN";
cc.Windows.ClientCredential.Password = "PASSWORD";
cc.Windows.ClientCredential.UserName = "USERNAME";

您可以在此处找到更多详细信息。

https://msdn.microsoft.com/zh-CN/library/microsoft.crm.sdk.messages.executeworkflowrequest.aspx

您可以像这样在js中调用工作流程:

您可以通过名称和类型定义查询工作流ID。

    var entityId = // The GUID of the entity
    var workflowId = // The GUID of the workflow
    var url = // Your organization root
    var orgServicePath = "/XRMServices/2011/Organization.svc/web";
    url = url + orgServicePath;
    var request;
    request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
        "<s:Body>" +
        "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
        "<request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">" +
        "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">" +
        "<a:KeyValuePairOfstringanyType>" +
        "<c:key>EntityId</c:key>" +
        "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + entityId + "</c:value>" +
        "</a:KeyValuePairOfstringanyType>" +
        "<a:KeyValuePairOfstringanyType>" +
        "<c:key>WorkflowId</c:key>" +
        "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + workflowId + "</c:value>" +
        "</a:KeyValuePairOfstringanyType>" +
        "</a:Parameters>" +
        "<a:RequestId i:nil=\"true\" />" +
        "<a:RequestName>ExecuteWorkflow</a:RequestName>" +
        "</request>" +
        "</Execute>" +
        "</s:Body>" +
        "</s:Envelope>";

    var req = new XMLHttpRequest();
    req.open("POST", url, false);
    // Responses will return XML. It isn't possible to return JSON.
    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.send(request);

如果request.status为200,则请求成功。 在CRM2011环境中对此进行了测试。

我建议您创建WCF rest或Web api ,引用IOrganizationService并从中使用CRM服务的操作。 最好直接调用中间WCF,而不是直接调用IOrganizationService。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM