Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
    
Do you get anything if you simplify your alerts to just 'Item Created' and 'request failed'? –  Dave Wise Feb 19 '13 at 21:40

1 Answer 1

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());
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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