I'm trying to add an item to a SharePoint list like so:
Title | Column1 | Column2
-------+-----------+-----------
title1 | 1 | a
title2 | 2 | b
Now lets say I want to add
title3 | 3 | c
Using the following function:
var listColumnNames = ["Column1", "Column2"];
function addNewItem(title, columns) {
var itemCreateInfo = new SP.ListItemCreationInformation();
var newListItem = list.addItem(itemCreateInfo);
// this works, it will create a new row with the title
newListItem.set_item("Title", title);
// this throws the error below
newListItem.set_item("Column1", "test");
// this does nothing
newListItem["Column1"] = "test";
// this is my ultimate goal, but also doesn't work
for (var i = 0 ; i < listColumnNames.length ; i++)
newListItem.set_item(listColumnNames[i], columns[i])
newListItem.update();
context.load(newListItem);
context.executeQueryAsync(querySuccess, queryFailed);
}
But I get this error:
Column 'Column1' does not exist. It may have been deleted by another user. /MyPath/MyListName undefined
In this tutorial: http://adicodes.com/add-update-and-delete-list-items-using-ecmascript/
It omits the lines I need, but it seems like what I'm doing is the right idea:
newItem.set_item('Title', 'This is new item');
// set values to other columns of the list here
newItem.update();
In the docs: https://msdn.microsoft.com/en-us/library/office/hh185011(v=office.14).aspx
It shows doing something that looks like what I'm doing, but it doesn't clarify whether "Body" is actually the column name or some other attribute I'm unaware of.
oListItem.set_item('Title', 'My New Item!');
oListItem.set_item('Body', 'Hello World!');
oListItem.update();
Can anyone see what I'm doing wrong?
Body
andTitle
are field internal names. Both the examples are valid. For testing I would suggest you comment out thefor
loop and check the item is getting created with the values.