Process multiple sharepoint list items

2.1k views Asked by At

in a SharePoint list view I would like to duplicate selected list items. My code works when only one item selected but fails, when more. Debugging the code I see that it goes through all selected items first before calls the success callback. Also within the success callback the currItem not always filled with the item data.

How can I get and process the selected items one-by-one?

function copySelected(){
if($("#copyAllButton").hasClass('ms-cui-disabled')){
    return;
}
var cc = null;
var web = null;
copyCounter = 0;
failedCounter = 0;
cc = new SP.ClientContext.get_current();
web = cc.get_web();
var currItem = null;
notifyId = SP.UI.Notify.addNotification(duplicatingItemsNotification, true);
var selectedItems; 
currList = web.get_lists().getById(SP.ListOperation.Selection.getSelectedList());
selectedItems = SP.ListOperation.Selection.getSelectedItems();
if( selectedItems.length > 0 ){
    for(var i in selectedItems){
        //var currItemID = selectedItems[i].id;
        currItem = currList.getItemById(selectedItems[i].id);
        cc.load(currItem);
        cc.executeQueryAsync(function(sender, args){
            var itemCreateInfo = new SP.ListItemCreationInformation();
            var aListItem = currList.addItem(itemCreateInfo);
                aListItem.set_item('Title', currItem.get_item('Title'));
                aListItem.set_item('Customer', currItem.get_item('Customer'));
                aListItem.set_item('Description', currItem.get_item('Description'));
                aListItem.set_item('Source', currItem.get_item('Source'));
                aListItem.set_item('field2', currItem.get_item('field2'));
                aListItem.set_item('field3', currItem.get_item('field3'));
                aListItem.set_item('Workloadtype', currItem.get_item('Workloadtype'));
                aListItem.set_item('Tickettype', currItem.get_item('Tickettype'));
                aListItem.set_item('Customergroup', currEngineer.group);
                aListItem.set_item('Allocation', currEngineer.allocation);
                aListItem.set_item('SubCap', currItem.get_item('SubCap'));
                aListItem.set_item('Engineer', currEngineer.fullName);
                aListItem.update();
                cc.load(aListItem);
                cc.executeQueryAsync(function(){
                    copyCounter ++;
                },function(){
                    failedCounter ++;
                });
        }, Function.createDelegate(this,this.getItemFailed));
    }
    notifyMe();
}

}

1

There are 1 answers

0
BigZolo On

In the meantime I found out the resolution (it was good to rethinking the problem for the question).

I fill an array of the required items with the query then processes the array.

 var allSelectedItems;

 function copySelected(){
if($("#copyAllButton").hasClass('ms-cui-disabled')){
    return;
}
var cc = null;
var web = null;
copyCounter = 0;
failedCounter = 0;
cc = new SP.ClientContext.get_current();
web = cc.get_web();
//var currItem = null;
notifyId = SP.UI.Notify.addNotification(duplicatingItemsNotification, true);
var selectedItems; 
currList = web.get_lists().getById(SP.ListOperation.Selection.getSelectedList());
selectedItems = SP.ListOperation.Selection.getSelectedItems();
if( selectedItems.length > 0 ){
    allSelectedItems = new Array(selectedItems.length);
    for(var i in selectedItems){
        allSelectedItems[i] = currList.getItemById(selectedItems[i].id);
        cc.load(allSelectedItems[i]);
    }
    cc.executeQueryAsync(Function.createDelegate(this, this.getItemSucceded), Function.createDelegate(this, this.getItemFailed));
    notifyMe();
}

}