0

SharePoint リスト ワークフロー ステータス列の値を変更する必要があります。

1 つのリストに 2 つ以上のワークフロー列を含めることができると仮定しましょう (以下のコードは、リストに 1 つのワークフロー ステータス列がある場合に機能します)。

問題は、複数のワークフロー ステータス列がある場合です。ネストされた executeQueryAsync 呼び出しがあるため、最後の列のみが更新されます。

Deferred/Promise でも同じことを試しました。2 番目のコード ブロックを参照してください。

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(initialize, "sp.js");

//Get our objects
function initialize() {
    var t0 = performance.now();
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getById(_spPageContextInfo.pageListId)
    var fieldCollection = list.get_fields();
    var spField;
    context.load(fieldCollection);
    context.executeQueryAsync(Function.createDelegate(this, onFieldCollectionSucceeded), Function.createDelegate(this, onListDataFailed));

    function onFieldCollectionSucceeded(sender, args) {
        var fieldEnumerator = fieldCollection.getEnumerator();
        while (fieldEnumerator.moveNext()) {
            var field = fieldEnumerator.get_current();
            var fType = field.get_fieldTypeKind();
            //Value 28 correspond to SPWorkflowStatus
            if (fType === 28) {
                console.log(fieldEnumerator.get_current().get_title() + ": " + fType);

                spField = field;
                context.load(spField, "SchemaXml");
                context.executeQueryAsync(Function.createDelegate(this, onFieldSucceeded), Function.createDelegate(this, onListDataFailed));
            }
        }
    }

    function onFieldSucceeded(sender, args) {
        console.log("Schema changes processing for column: " + spField.get_title());
        var schema = spField.get_schemaXml();
        var newSchema = schema.replace('Abgelehnt', 'Rejected');
        spField.set_schemaXml(newSchema);
        spField.update();
        context.executeQueryAsync(
           Function.createDelegate(this, schemaChanged),
           Function.createDelegate(this, onListDataFailed));
    };

    function schemaChanged(sender, args) {
        console.log('Field Schema Updated');
    }

    function onListDataFailed(sender, args) {
        console.log('List Data fetch failed. ' + args.get_message() + 'n' + args.get_stackTrace());
    };

    var t1 = performance.now();
    console.log("Total Execution Time " + (t1 - t0) + " milliseconds.")
};

<script type="text/javascript">
//http://johnliu.net/blog/2015/12/convert-sharepoint-jsoms-executequeryasync-to-promise-in-the-prototype
ExecuteOrDelayUntilScriptLoaded(registerJsomPromise, "sp.js");
ExecuteOrDelayUntilScriptLoaded(initialize, "sp.js");

function initialize() {
    var t0 = performance.now();
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getById(_spPageContextInfo.pageListId)
    var fieldCollection = list.get_fields();
    var spField;
    context.load(fieldCollection);
    context.executeQueryAsync(Function.createDelegate(this, onFieldCollectionSucceeded), Function.createDelegate(this, onListDataFailed));
};

function onFieldCollectionSucceeded(sender, args) {
    var fieldEnumerator = fieldCollection.getEnumerator();
    while (fieldEnumerator.moveNext()) {
        var field = fieldEnumerator.get_current();
        var fType = field.get_fieldTypeKind();
        //Value 28 correspond to SPWorkflowStatus
        if (fType === 28) {
            console.log(fieldEnumerator.get_current().get_title() + ": " + fType);

            spField = field;
            context.load(spField, "SchemaXml");

            var promise = context.executeQuery();
            setTimeout(function () { }, 500);

            promise.done(function () {
                console.log("Schema changes processing for column: " + spField.get_title());
                var schema = spField.get_schemaXml();
                var newSchema = setSchema(schema);
                spField.set_schemaXml(newSchema);
                spField.update();
                context.load(spField);

                context.executeQueryAsync(
                   Function.createDelegate(this, schemaChanged),
                   Function.createDelegate(this, onListDataFailed));
            });
            promise.then(function (sArgs) {
                console.log('Field Schema Updated');
                //sArgs[0] == success callback sender
                //sArgs[1] == success callback args
            }, function (fArgs) {
                //fArgs[0] == fail callback sender
                //fArgs[1] == fail callback args.
                //in JSOM the callback args aren't used much - 
                //the only useful one is probably the get_message() 
                //on the fail callback
                var failmessage = fArgs[1].get_message();
            });
            //context.executeQueryAsync(Function.createDelegate(this, onListDataSucceeded), Function.createDelegate(this, onListDataFailed));
        }
    }
}

function schemaChanged(sender, args) {
    console.log('Field Schema Updated');
}

function onListDataFailed(sender, args) {
    console.log('List Data fetch failed. ' + args.get_message() + 'n' + args.get_stackTrace());
};

var t1 = performance.now();
console.log("Total Execution Time " + (t1 - t0) + " milliseconds.")


function registerJsomPromise() {

    SP.ClientContext.prototype.executeQuery = function () {
        var deferred = $.Deferred();
        this.executeQueryAsync(
            function () { deferred.resolve(arguments); },
            function () { deferred.reject(arguments); }
        );
        return deferred.promise();
    };


}

4

1 に答える 1