0

For the past days I've been trying, with no success, to establish a connection to a sharepoint list (https://mywebsite.com/sites/concasbkcu/Lists/list_teste/AllItems.aspx) and update its items with the follow code:

var siteUrl= 'https://mywebsite.com/sites/concasbkcu/';
var targetlistItem;
var itemID = 1;

function updateListItem() {

var clientContext = new SP.ClientContext(siteUrl);
var targetList = clientContext.get_web().get_lists().getByTitle('list_teste');

targetListItem = targetList.getItemById(itemID);

targetList.set_item('Title', 'Test Title');

targetList.update();

clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);

}

function onQuerySucceeded() {

    alert('Item updated!');
}

function onQueryFailed() {

    alert('Request failed');
}

I tried calling this function on a html button, but it just doesn't work...no error message is shown and the item doesn't update at all. Can someone help me, please?

Regards!

2 Answers 2

0

Please update your code as below.. It will work.

$(document).ready(function () 
        {
            $("#UpdateButton").click(function(){
                SP.SOD.executeFunc('sp.js', 'SP.ClientContext', updatelistitem);            
            });
        });
        var oListItem ="";
        function updatelistitem() { 
        var itemid = "1";
        var clientContext = new SP.ClientContext.get_current();  
        var oList = clientContext.get_web().get_lists().getByTitle('City');  
        oListItem = oList.getItemById(itemid); 
        oListItem.set_item('Title', 'New City Name');
        oListItem.set_item('Status', 'InActive');
        oListItem.update();  
        clientContext.load(oListItem);  
        clientContext.executeQueryAsync(onQuerySucceeded,onQueryFailed);  
    }  
        function onQuerySucceeded() {           
            alert("Item Updated Sussefully !!!");
        }
        function onQueryFailed(sender, args) {  
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
        }
0

In your code, you need to use

targetListItem.set_item('Title', 'Test Title');

targetListItem.update();

instead of

targetList.set_item('Title', 'Test Title');

targetList.update();

And change clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed); to clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

5
  • Hi Michael! Thanks for you answer, id didn't however, work for me...still no message appears and the item isn't updated :(
    – Dessiog
    Commented Aug 31, 2021 at 11:21
  • Press F12, check if there is any error in console. Commented Sep 1, 2021 at 7:46
  • Hi Michael, it appeared to me: Uncaught ReferenceError: SP is not defined at updateListItem (tst.js:7) at HTMLInputElement.onclick (index_teste.html:50)
    – Dessiog
    Commented Sep 1, 2021 at 10:59
  • Where did you run the code? You should add the code in a classic sharepoint page. Commented Sep 2, 2021 at 1:16
  • I'm running on a sharepoint page...
    – Dessiog
    Commented Sep 2, 2021 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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