3

I have the following code in my SharePoint 2013 app, I'm getting no errors but the item is not being added, anyone spot anything eyes are square from looking!

function addListItem() {

this.web = ctx.get_web();
this.List = this.web.get_web().get_lists().getByTitle('ListName');


var itemCreateInfo = new SP.ListItemCreationInformation();
this.ListItem = this.List.addItem(itemCreateInfo);
this.ListItem.set_item('Title', 'This is my test holiday title!');
this.ListItem.set_item('StaffName', 'Joe Black');
this.ListItem.update();

this.ctx.load(this.ListItem);
this.ctx.executeQueryAsync(
    Function.createDelegate(this, this.onQuerySucceeded), 
    Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded() {
alert('Item created: ' + ListItem.get_id());
}
 function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + 
    '\n' + args.get_stackTrace());
}

TIA

Stephen
  • 1,812
  • 8
  • 35
  • 56

1 Answers1

6

Where do you define ctx? You need to either get it via

ctx = new SP.ClientContext.get_current() 

or via

ctx = new SP.ClientContext(siteUrl);

Straight from MSDN

function createListItem(siteUrl) {
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Announcements');

        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItem = oList.addItem(itemCreateInfo);
        oListItem.set_item('Title', 'My New Item!');
        oListItem.set_item('Body', 'Hello World!');
        oListItem.update();

        clientContext.load(oListItem);
        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded), 
            Function.createDelegate(this, this.onQueryFailed)
        );
    }

    function onQuerySucceeded() {
        alert('Item created: ' + oListItem.get_id());
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + 
            '\n' + args.get_stackTrace());
    }
Marius Constantinescu
  • 12,771
  • 16
  • 28