0

Following is my code using Rest API to update a List Item. It generates no error, goes to success callback. However it does not update the List Item.

var itemTitle = "Test item";
var userId = 22; //it is true the user exists
var now = new Date();
var date = now.getDate() + "/" + now.getMonth() + 1 + "/" + now.getFullYear();
var time = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
var newMessage = "<u>" + currentUserName + " <u>changed: " + date + " " + time + "</u><br/>-----------------------<br/>";

var listItemUri = webUrl + "/_api/web/lists/GetByTitle('MyList')/items?$filter=Title eq '" + itemTitle + "'";

var properties = {
      '__metadata': { "type": "SP.Data.MyListListItem" }, //checked this is right
       "Title": itemTitle,
       "MyUser": userId,
        "Last_x0020_Updated": now,
        "MyMessage": newMessage
   };

    $.ajax({
       url: listItemUri,
       type: "POST",
       contentType: "application/json;odata=verbose",
       data: JSON.stringify(properties),
       headers: {
           "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            console.log(JSON.stringify(data.d));
             alert("updated");
         },
         error: function (data) {
            console.log(data.responseText);
            alert("Failed");
        }
   });
1

Try URL

webUrl + "/_api/web/lists/GetByTitle('LoggingList')/items(1)"

Where 1 is the ID of item to update

  • Does it mean it will update all items? – Medes Jun 12 '15 at 10:28
  • No for updating you need to specify which item. See my edit – Amal Hashim Jun 12 '15 at 10:29
  • the problem is I don't know the id of the item, I know the title – Medes Jun 12 '15 at 10:30
  • Ok then you need to make two calls.. First GET item using filter, find the ID and then use POST to update – Amal Hashim Jun 12 '15 at 10:32
  • so it doesn't enough to get the item using filter and title? – Medes Jun 12 '15 at 10:33

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.