4

I am trying to update list item's value through JavaScript object model in SP 2013. When I call set_item('fieldname','mvalue'), it does not throw any error, but it fails to update. How to successfully update a ListItem? I am able to get the current item id, and after set_item() is getting called, my alert('testing for update...') is also working, but it seems that some problem exists in clientContext.executeQueryAsync() or load() event.here is my code:

$(document).ready(function() {    
    ExecuteOrDelayUntilScriptLoaded(GetDesc, "sp.js");
});

function GetDesc()
{
    context = new SP.ClientContext.get_current();
    web = context.get_web();
    siteURL=$(location).attr('href');
    var titleName=document.title;
    var mListName=  (titleName.substring(0,titleName.lastIndexOf("-"))).trim();
    mList = web.get_lists().getByTitle(String(mListName));
    context.load(mList);
    alert(titleName+"!");
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
    '<Value Type=\'Number\'>1</Value></Geq></Where></Query>
     <RowLimit>10</RowLimit></View>');
    this.collListItem = surveyList.getItems(camlQuery);
    context.load(collListItem);
    context.executeQueryAsync(Function.createDelegate(this, 
        this.onQuerySucceeded), Function.createDelegate(this, 
        this.onQueryFailed));
    alert("Else");
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        alert("while");
        var curritem = listItemEnumerator.get_current();
        alert(curritem.get_id());
        curritem.set_item("Testing","Test");
        alert("Updated");
        curritem.update();
        alert("Updated1");
        //clientContext.executeQueryAsync(onQuerySucceeded1, onQueryFailed1);
        //clientContext.load(curritem);
        clientContext.executeQueryAsync(Function.createDelegate(this, 
            this.onQuerySucceeded), Function.createDelegate(this, 
            this.onQueryFailed));
        alert("Updated2");
        //clientContext.executeQuery();
    }
}
function onQuerySucceeded(sender, args) {
    var desc = surveyList.get_description();
    desc=desc.trim();
    if(desc.lastIndexOf("Closed") != -1)
    {
        alert("Quiz has been closed");
        //window.location.assign("/sites/Test/SitePages/Home.aspx");
        //window.location.assign(window.parent.location.href);
        //window.parent.location.Reload();
        //window.location.Reload();
        window.location.assign(siteURL.substring(0,
        siteURL.lastIndexOf("Lists/")));
    }   else    {
        alert("Updated"); 
    }
6

I have corrected your code and here you go

$(document).ready(function() {    
       ExecuteOrDelayUntilScriptLoaded(GetDesc, "sp.js");
       });


    function GetDesc()
  {

   context = new SP.ClientContext.get_current();
    web = context.get_web();
    siteURL=$(location).attr('href');
    var titleName=document.title;
    var mListName=  "TestList";
    mList = web.get_lists().getByTitle(String(mListName));
      context.load(mList);

     var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
      this.collListItem = mList.getItems(camlQuery);
    context.load(collListItem);
    context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded1), Function.createDelegate(this, this.onQueryFailed1));


     }


     function onQuerySucceeded1() {

    alert('Got the collection!');

 var listItemEnumerator = collListItem.getEnumerator();
      while (listItemEnumerator.moveNext()) {

      var curritem = listItemEnumerator.get_current();
        alert(curritem.get_id());
     curritem.set_item("Title","Test");
      alert("Updated");
        curritem.update();
        alert("Updated1");

        context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

        }
}

function onQueryFailed1(sender, args) {

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


function onQuerySucceeded() {

    alert('Item updated!');
}

function onQueryFailed(sender, args) {

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

Let me know if you have any questions.

4

Updating a list item in JSOM should be as simple as this:

function updateListItem(itemId) {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('My List');

    this.oListItem = oList.getItemById(itemId);

    oListItem.set_item('MyField', 'My Updated Field Value');

    oListItem.update();

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Item updated!');
}

function onQueryFailed(sender, args) {

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

Since you are retrieving a group of items, I'd probably do something like:

function updateListItem() {    
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('My List');
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
    this.collListItem = oList.getItems(camlQuery);
    clientContext.load(collListItem);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuery1Succeeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuery1Succeeded() {
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        oListItem.set_item('MyField1', 'My Updated Field Value');
        oListItem.set_item('MyField2', 'Hello World!');

        oListItem.update();
    }
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Items updated!');
}

function onQueryFailed(sender, args) {

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

NO need of "context.load(mList);" simply loading the ListItemCollection will bring the item collection. During itration process is going on you cannot make any changes into the item. Hence updation action should be seperated from itration process calling loop.

  • He's alerting the list title, which does need the context.load(mList). Though that's the only use of the title, so he might not really need it. – wjervis Jul 2 '15 at 13:53

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.