Unexpected Response from Server when updating SharePoint ListItem via JSOM
June 22, 2016 Leave a comment
These days I am working a lot on the client-side of things. So a couple of months ago I started writing my first lines of JavaScript/ JSOM (Javascript (Client)-Side Object Model).
I wrote a small method to create list items in a list (listTitle) based on a collection/ list of properties (properties) and their respective values. Here it is:
function createListItem(listTitle, properties) { var d = $.Deferred(); try { var ctx = SP.ClientContext.get_current(); var oList = ctx.get_web().get_lists().getByTitle(listTitle); var itemCreateInfo = new SP.ListItemCreationInformation(); oListItem = oList.addItem(itemCreateInfo); for (var i = 0; i < properties.length; i++) { var prop = properties[i]; oListItem.set_item(prop.Key, prop.Value); } oListItem.update(); ctx.load(oListItem); var o = { d: d, ListItem: oListItem, List: listTitle }; ctx.executeQueryAsync( function () { o.d.resolve(o.ListItem); }, function (sender, args) { o.d.reject("Could not create list item in list " + o.List + " - " + args.get_message()); }); } catch (Exception) { console.log(Exception.message); } return d.promise(); } function updateListItem(listTitle, properties) { var d = $.Deferred(); try { var d = $.Deferred(); var ctx = SP.ClientContext.get_current(); var oList = ctx.get_web().get_lists().getByTitle(listTitle); oListItem = oList.getItemById(id); for (var i = 0; i < properties.length; i++) { var prop = properties[i]; try { oListItem.set_item(prop.Key, prop.Value); } catch (Exception) { console.log(prop.Key + ' ' + prop.Value); console.log(Exception.message); } } oListItem.update(); //ctx.load(oListItem); var o = { d: d, ListItem: oListItem, List: listTitle, p: properties }; ctx.executeQueryAsync( Function.createDelegate(o, function () { o.d.resolve(o.ListItem); }), Function.createDelegate(o, function (sender, args) { o.d.reject("Could not update list item in list " + o.List + " - " + args.get_message()); })); } catch (Exception) { console.log(Exception.message); } return d.promise(); }
So this is what happened when I had this code execute on editing another item in a different list…
When I debugged using Chrome (my browser of choice when writing JavaScript – never used it before that, interestingly…) I received the error “unexpected response from the server”.
I figured out that there are two key lines in this code that can be the cause of this.
var ctx = SP.ClientContext.get_current();
and
ctx.load(oListItem);
In my case the first line was actually not responsible for the error message. For your reference if you use var ctx = new SP.ClientContext(url); you may encounter this error message. So make sure to check that.
You should always use the current client context, when using JSOM, similar to the best practice guidelines for opening webs on server-side (SSOM [Server-Side Object Model]).
In my case the second line was the cause for the issue.
When creating an item I need to load the item into the context afterwards (or the error will show up even if the item is created correctly).
When updating an item the item may not be loaded into the context afterwards (or the error will show up even if the item is updated correctly).
It kind of makes sense, because when creating an item you are actually sending an SP.ListItemCreationInformation to the server. When updating an item I already have my listitem object. Why would I need to load all the other information afterwards?
So once I removed the line from the update method the code no longer evaluated to fail and the error message disappeared.
So for the experts among you this may be old news, but I actually needed to think about this for a few minutes before I figured it out, so I thought it was well worth blogging about. Especially since I haven’t blogged for quite some time.