0

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.

2

You need to use the internal name of the column. The internal name is assigned at the time of column creation and can't be changed.

Any special characters or spaces get encoded when you create a column, so AIT# has an internal name of AIT_x0023_.

For this reason we always make sure to create columns without spaces, no special characters, only letters and numbers, and don't start with a number. After the column is created you can then go back and edit the display name to something more human readable.

| improve this answer | |
  • It looks like this was the issue. Thank you! – user87293 Nov 14 '19 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.