在重定向之前等待工作流程完成

时间:2014-08-12 21:32:59

标签: javascript jquery sharepoint sharepoint-2013

我需要在工作流程完成后将用户重定向到新页面。这是必需的,因为我在输入新表单数据后将用户重定向到显示表单,而不是将它们返回到所有项目视图。我需要完成工作流程,因为它会更新字段值和安全性。

目前,我将它们发送到具有JavaScript函数的New Form的重定向页面,以检查Workflow状态的列是否已更改为true。要循环和检查,我只是再次调用相同的功能......这是非常非常糟糕的做法!使用jQuery,我如何检查SharePoint Workflow是否完成?我的初始代码破解如下:

SP.SOD.executeFunc("sp.js", 'SP.ClientContext', redirectNewItem);

function redirectNewItem() {

// REST call to get the current user
    $.ajax({
        async: false,
        url: "/sites/mysite/_api/web/currentUser",
        type: "GET",
        headers: { "Accept": "application/json;odata=verbose" },
        success: function (user) {

            //Get the user ID
            var userID = user.d.Id;

            // REST call to pass in the current user to get the most recent item created by the user. This is to ensure that the redirect goes to the item they just created after the workflow setting field values and security has completed.
            $.ajax({
                async: false,
                url: "/sites/mysite/_api/web/lists/getbytitle('mylist')/items?$filter=AuthorId eq " + userID + "&$top=1&$orderby=Created desc",
                type: "GET",
                headers: { "Accept": "application/json;odata=verbose" },
                success: function (data) {

                    //Get the user ID
                    $.each(data.d.results, function (index, value) {
                        var itemID = value.Id;

                        var redirectURL = "/sites/mysite/Lists/mylist/DispForm.aspx?ID=" + itemID

                        if (value.WorkflowComplete == "True") {
                            // Redirect to display form with correct ID

                            window.location.replace(redirectURL);
                        }
                        redirectNewItem();
                    });

                },
                error: function (data) {
                    //output error HERE
                    alert(data.statusText);
                }
            });
        },
        error: function (user) {
            //output error HERE
            alert(data.statusText);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

我通过更改函数调用来使其工作,包括5秒的超时。这循环直到条件语句为真。我还更新了我的代码以从页面上下文中获取userID,而不是使用REST调用:

SP.SOD.executeFunc("sp.js", 'SP.ClientContext', redirectNewItem);

function redirectNewItem() {

//Get the user ID
var userID = _spPageContextInfo.userId;

// REST call to pass in the current user to get the most recent item created by the user. This is to ensure that the redirect goes to the item they just created after the workflow setting field values and security has completed.
$.ajax({
    async: false,
    url: "/sites/mysite/_api/web/lists/getbytitle('mylist')/items?$filter=AuthorId eq " + userID + "&$top=1&$orderby=Created desc",
    type: "GET",
    headers: { "Accept": "application/json;odata=verbose" },
    success: function (data) {

        //Get the user ID
        $.each(data.d.results, function (index, value) {
            var itemID = value.Id;

            var redirectURL = "/sites/mysite/Lists/mysite/DispForm.aspx?ID=" + itemID

            if (value.WorkflowComplete == "True") {
                // Redirect to display form with correct ID

                window.location.replace(redirectURL);
            }
            setTimeout(redirectNewItem, 5000);
        });

    },
    error: function (data) {
        //output error HERE
        alert(data.statusText);
    }
});

}