1

I'm trying to update a single value of a list item in SharePoint 2010 using REST and jQuery. So far, I haven't gotten anything to work. I've looked at many posts and MSDN articles. Here's a link to a post that claims to work, but it fails with "500: Internal Server Error". Below is my version of the code To be clear, I need to do everything from the client, and would prefer to use REST over SPServices method.

<html>
<head>
<script language="javascript" type="text/javascript" 
src="https://http://[path removed]/jquery.json-2.4.min.js"></script>
<script language="javascript" type="text/javascript">

//update
           function updateProject(id) {
               var projectUrl = "http://[path removed]/_vti_bin/listdata.svc/ProjectManagement";
               projectUrl = projectUrl + "(" + id+ ")";
               var beforeSendFunction;  
               var projectModifications = {};
               projectModifications.Title = "Test Update Performed";
               //var updatedProjectData = JSON.stringify(projectModifications);
               var updatedProjectData = $.toJSON(projectModifications); 
               //update exsiting project
               beforeSendFunction = function (xhr) {
                   xhr.setRequestHeader("If-Match", "*");
                   // Using MERGE so that the entire entity doesn't need to be sent over the wire.
                   xhr.setRequestHeader("X-HTTP-Method", 'MERGE');
               };

               $.ajax({
                   type: "POST",
                   contentType: "application/json; charset=utf-8",
                   processData: false,
                   beforeSend: beforeSendFunction,
                   url: projectUrl,
                   data: updatedProjectData,
                   dataType: "json",
                   error: function (xhr) {
                       alert(xhr.status + ": " + xhr.statusText);
                   },

                   success: function () {
                       alert("Updated");
                       getAll();
                       }
               });
           }    
</script>
<title>Test Page</title>
</head>
<body>
<div id="divarea">This is a test page</div>
<button onclick="updateProject(2523);">Update!</button>
</body>
</html>
Community
  • 1
  • 1
TimS
  • 13
  • 6

1 Answers1

1

The only possible issue that I've found in your example is related with List Item object projectModifications, the error occurs when List Item entity does not contain the specified property (in your case Title).

For example, in case of OOTB Tasks list ( entity Microsoft.SharePoint.DataService.TasksItem) you should specify TaskName property:

var taskProperties = {
        'TaskName': 'Approval'
    };

So, the solution, make sure that you specify the correct property.

In addition, you could print the detailed error message like this:

error: function (xhr) {
   console.log(JSON.stringify(xhr.responseJSON.error));
}

Below is provided the common function for performing an Update operation

function updateListItem(webUrl,listName,itemId,itemProperties,success, failure)
{ 
      var itemUrl = webUrl + "_vti_bin/listdata.svc/" + listName + "(" + itemId + ")"; 
      $.ajax({
         type: 'POST',
         url: itemUrl,
         contentType: 'application/json',
         processData: false,
         headers: {
                "Accept": "application/json;odata=verbose",
                "X-HTTP-Method": "MERGE",
                "If-Match": "*" 
         },
         data: JSON.stringify(itemProperties),
         success: function () {
                success();
         },
         error: function (data) {
                failure(data.responseJSON.error);
         }
      });

}


//Usage: how to update Task Name (Title) in Tasks list  
var taskProperties = {
    'TaskName': 'Approval'
};


updateListItem('https://contoso.sharepoint.com/project/','Tasks',17,taskProperties,function(){
    console.log('Task has been updated'); 
  },
  function(error){
    console.log(JSON.stringify(error));
  }
); 

References

Please follow an article List Items manipulation via REST API in SharePoint 2010 that contains a description of CRUD operations for SharePoint 2010 REST API.

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • 1
    Wow! That worked! I've been trying all sorts of adjustments to the code I posted, but when I used your example it worked the first time. Many thanks! I wish I knew what the problem was with the old code. – TimS Apr 03 '14 at 16:00
  • Great, glad to hear :) – Vadim Gremyachev Apr 03 '14 at 16:04
  • I'm still trying to figure out the add/update for item with a multiple select lookups. Any examples on that? – Joe Johnston Jul 10 '16 at 16:20