I am adding JavaScript to a SharePoint site and part of its functionality is to insert items into a SharePoint list. I have previously accomplished this with a single column ("Title") and I'm trying to update it to insert multiple columns for each new item (specifically, "Title", and "AIT#"). Here is the function I am using:
/**
* Inserts new items to a List
*
* @params
* @params string listName
* @params string[] listItemFields
*
* @returns void
**/
function insertListItems(newListItems, listName, listItemFields) {
console.log(newListItems);
var clientContext = new SP.ClientContext.get_current();
var olist = clientContext.get_web().get_lists().getByTitle(listName);
// Get SP list
var itemCreateInfo = new SP.ListItemCreationInformation();
for(var i = 0; i < newListItems.length; i++) {
oListItem = olist.addItem(itemCreateInfo);
oListItem.set_item("Title", newListItems[i]["Title"]);
oListItem.set_item("AIT#", newListItems[i]["AIT#"]);
// for(var j = 0; j < listItemFields.length; j++) {
// oListItem.set_item(listItemFields[j], newListItems[i][listItemFields[j]]);
// }
oListItem.update();
}
clientContext.load(oListItem);
clientContext.executeQueryAsync(
this.successHandler("Items inserted to " + listName),
this.errorHandler()
);
}
When I only use the Title column, it works perfectly, even with multiple items. But when add the AIT# column (which is a custom column I added to the List), the response reports success but the list does not include any of the items that were supposedly added. Can anyone offer any insight as to why this is?
Also, on an unrelated note, I've noticed that when the function is querying SharePoint like this, after the successHandler is called the errorHandler is also called, even if the response is a success.