如何在没有Javascript循环的情况下访问SubscriptionCollection中的订阅

问题描述 投票:-1回答:1

我有以下代码可行:

ctx.load(workflowAssociations);

//below works but loops through all available workflows, it seems sub may contain the array that needs to be accessed

ctx.executeQueryAsync(function (sender, args) {

    var subsEnum = workflowAssociations.getEnumerator();

    while (subsEnum.moveNext()) {

        var sub = subsEnum.get_current();

        alert(sub);

        console.log('Web: ' + web.get_url() + ', Subscription: ' + 
          sub.get_name() + ', id: ' + sub.get_id());

        var initiationParams = {};
        workflowServicesManager.getWorkflowInstanceService().startWorkflowOnListItem(
          sub, items.getItemAtIndex(0).get_id(), initiationParams);

        ctx.executeQueryAsync(function (sender, args) {
            console.log('Workflow started.');
        }, errFunc);

    }

}, errFunc);


function errFunc(sender, args) {
alert("Error occured! " + args.get_message() + 
         '\r\nStack trace: ' + args.get_stackTrace());
}

我试图简化这个循环,只访问集合中的一个对象而没有while循环。我尝试过以下方法:

//var subsEnum = workflowAssociations.getEnumerator();
console.dir(subsEnum);
console.dir(workflowAssociations[2]);
console.log(subsEnum[2]);
var sub = subsEnum.get_current(0);
console.dir(sub);
console.dir(subsEnum);

但是,大多数这些都是未定义的。下面是使用chrome中的手表表达能力探索对象时的图像。我不想使用那个$ 2_0的东西,因为在迁移中我认为它可能会改变。

我提前为任何缺乏信息而道歉。

enter image description here

javascript arrays rest sharepoint sharepoint-workflow
1个回答
0
投票

SP.Workflow.WorkflowAssociation class(在你的例子中是workflowAssociations)来自SP.ClientObjectCollection class,除了getEnumerator() method之外还包含用于访问收集数据的some additional methods,例如:

  • getItemAtIndex - 获取SP.ClientObjectCollection类的指定索引处的元素。
  • getData - 将ClientObjectCollection对象转换为常规JavaScript数组

例子

枚举集合

var e = workflowAssociations.getEnumerator();
while(e.moveNext()){
    console.log(e.get_current().get_name()); //print Name of current WorkflowAssociation object     
}

获取指定索引处的项目

 var item = workflowAssociations.getItemAtIndex(0); //get first item
 console.log(item.get_name()); //print Name of WorkflowAssociation object

转换为数组

var items = workflowAssociations.get_data();
console.log(items[0].get_name()); //print Name of first WorkflowAssociation object 
© www.soinside.com 2019 - 2024. All rights reserved.