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

1
  • Do you get anything if you simplify your alerts to just 'Item Created' and 'request failed'? Commented Feb 19, 2013 at 21:40

1 Answer 1

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());
    }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.