0

I am trying to create an app which posts information to a SharePoint List.

function createListItem() {

    try {
        var userName = document.getElementById("name").value;

        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('LeaveRequest');
        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItem = oList.addItem(itemCreateInfo);

        oListItem.set_item('Title', userName);

        oListItem.update();
        clientContext.load(oListItem);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
    catch (err)
    {
        alert(err.message);
    }
}

function onQuerySucceeded() {

    alert('Item created Successfully! ' + oListItem.get_id());

}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

When I click the submit button on the form, I am getting an alert to say the item has been added and also an id for the item in the list. However when I check the list the item is not there.

  • Your code works within my SharePoint solution. Please try again, copy the new ID and go the LeaveRequest list page, open any item and change the ID parameter value in the URL with your previously created item ID. – Salah Dec 10 '18 at 15:49
  • Hi @Salah ive tried that too, it just results in "page not found" – Niall Cooley Dec 10 '18 at 15:55
  • Probably that's permissions things.. Try to run the code using site collection admin account and check that an item was created. – Sergei Sergeev Dec 10 '18 at 16:12
  • 1
    May be you create list item in not expected by you list ? What relative path to your form with script ? What relative path to list where you try to find newly created item ? Or may be second thing - how you check that item is created in list ? What you do ? May be you open list view where some custom filter options applied. – user80188 Dec 10 '18 at 16:35
0

Based on your code, you created list item in add-in web(URL like https://tenant-d3bbc7b89c212c.sharepoint.com/sites/Developer/addinName/) not host web(https://tenant.sharepoint.com/sites/developer).

So, confirm the list item in add-in web (https://tenant-d3bbc7b89c212c.sharepoint.com/sites/Developer/addinName/lists/LeaveRequest)

Check official guideline to create list item in host web.

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.