2

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

  • Do you get anything if you simplify your alerts to just 'Item Created' and 'request failed'? – Dave Wise Feb 19 '13 at 21:40
5

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

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.