1

So I learned and have used the following to update a list item by ID using Javascript Client Object Model:

function updateItem(passedID) {

var ctx = new SP.ClientContext.get_current();
var myList = ctx.get_web().get_lists().getByTitle('Example');
this.listItem = myList.getItemById(passedID);
this.listItem.set_item("Title", "different value");
this.listItem.update();
ctx.executeQueryAsnyc(Function.createDelegate(this, this.OnHooray), Function.createDelegate(this, this.onAwww));
}

function onHooray(sender, args) {
alert('hooray');
}

function onAwww(sender, args) {
alert('Awww');
}

So this is the basic idea, but I would am interested in finding out if I can apply a CAML query instead of updating by the Id in every instance. So for example if I had a ProjectID within the example list could I somehow apply this:

var proproductIDVal = "TR543A1Z";

"<Query><Where><Eq><FieldRef Name = 'ProjectID'/><Value Type='Text'>" + productIDVal + </Value></Eq></Where>"</Query>";

so that whatever Id that has a record gets updated instead of knowing (or querying for) the record Id everytime in advance? Is this is possible would and there were multiple records would that update every record that held that value?

greatly appreciated as I can't seem to find a reference to this in particular anywhere.

1
  • How about I have a for loop outside the CamlQuery, and need to update the list field by the query result inside the loop? Only last listItem in the loop got update, and i received error 'Request Failed. version conflict'.. thanks for any help.
    – user19226
    Aug 30, 2013 at 2:33

1 Answer 1

5

One oddity about the JSOM is that you need to wrap the CAML query in a <View> tag.

var proproductIDVal = "TR543A1Z";

var queryXml = "<View><Query><Where><Eq><FieldRef Name = 'ProjectID'/><Value Type='Text'>" + productIDVal + "</Value></Eq></Where></Query></View>";
var query = new SP.CamlQuery();
query.set_viewXml(queryXml);
var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(listTitle);
var query = new SP.CamlQuery();
var items = list.getItems(query);
context.load(items);
context.executeQueryAsync(/**etc**/);

Then in your success callback you will need to take the items as a collection and loop through them.

var resultsEnumerator = items.getEnumerator();
while (resultsEnumerator.moveNext()) {
   var currentItem = resultsEnumerator.get_current();
   //Do stuff with the item and call update.
} 

//Call execute query async again, etc... 

Of course you can find plenty of examples online to get a full code example but this gives you the basics of how to do what you want.

Links: Getting List Items in JavaScript

Approve multiple documents or list items in one operation with client side code

5
  • appreciated Rob. I am familiar with getting list items approach, but I guess what I am asking is can I combine the get using caml set_viewXml in the this.update() approach so that I don't have to loop through items in callback and send updates for each item that I want to update....can caml be used in the update approach to send it once and updated everything using a reference to another field aside from ID, or can that approach only work with the list items id.
    – Justin
    Jun 28, 2013 at 14:12
  • hope that makes sense...appreciate the help
    – Justin
    Jun 28, 2013 at 14:12
  • Yes, so if you look at my code example I say in a comment as you loop through the items using the while and resultsEnumerator you make your changes on the currentItem and call update and then you call executeQueryAsync again. Jun 28, 2013 at 14:15
  • Just to be clear, you have to get the items before you can operate on them. You cannot use the CAML query like a SQL update statement if that is what you mean. Jun 28, 2013 at 14:16
  • 1
    ah...thanks that is exactly what I was wondering. makes sense. thanks once again Rob
    – Justin
    Jun 28, 2013 at 14:50

Your Answer

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

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