0

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?

1
  • Body and Title are field internal names. Both the examples are valid. For testing I would suggest you comment out the for loop and check the item is getting created with the values. Feb 3 '15 at 23:36
1

Make sure you are using field internal name.

You can find the field internal name by opening the List Settings. And then right click the field and copy the URL. Paste the URL in notepad and check the query string value for Field=

3
  • Got it! Thank you! Man, I feel like the examples should have made it more clear it was going to be 4 random characters. Feb 3 '15 at 23:51
  • 1
    Always use the field internal name. That is the rule of thumb. The reason being display name can get changed while internal name stays the same. Feb 3 '15 at 23:52
  • Ah! Thank you again. This also fixed another problem I was having with reading the list using listItem.get_item(). I was using the column names not the internal field names. Feb 4 '15 at 0:25

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.