简体   繁体   中英

adding more than one list item into List at a time when the items are generated dynamically (using NAPA)

I am using the below code to insert an item into the list.

    function createitem() {
     var selectListBox = document.getElementById("txtFormName");
     var selectedListTitle = selectListBox.value;
     var selectedList = web.get_lists().getByTitle(selectedListTitle);

     var listItemCreationInfo = new SP.ListItemCreationInformation();
     var newItem = selectedList.addItem(listItemCreationInfo);

     newItem.set_item('Title','abc');
     newItem.update();
     context.load(newItem);
     context.executeQueryAsync(onItemCreationSuccess, onItemCreationFail);
     }

Its working fine. Now I want to use

    newItem.set_item('Title1','def');
    newItem.set_item('Title2','xyz');
    .
    .
    .
    and so on

in loop for more than one item in the same row to insert. How can i achieve that. kindly guide.

Since SharePoint CSOM API supports Request Batching the following example demonstrate how to create multiple list items using a single request to the server:

function createListItem(context,listTitle,itemProperties)
{
    var web = context.get_web();
    var list = web.get_lists().getByTitle(listTitle);
    var itemCreateInfo = new SP.ListItemCreationInformation();
    var listItem = list.addItem(itemCreateInfo);

    for(var propName in itemProperties) {
       listItem.set_item(propName, itemProperties[propName]) 
    }
    listItem.update();
    return listItem;
}



//Usage
var contactItems = [];
var context = new SP.ClientContext.get_current();
var contactProperties  = {'Title': 'Doe','FirstName': 'John'};

//1.Prepare multiple list items
for(var i = 0; i < 16; i++) {
   var contactItem = createListItem(context,'Contacts',contactProperties);
   contactItems.push(contactItem);
}   
//2. Submit request to the server to create list items
context.executeQueryAsync(
      function() {
          console.log(contactItems.length + ' contacts have been created');        
      },
      function(sender, args) {
        console.log(args.get_message());
      }
   );

Key points:

  • List Item creation operations are submitted to the server in a single request using SP.ClientContext.executeQueryAsync method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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