This project has moved and is read-only. For the latest updates, please go here.

UpdateListItems and CAMLQuery

Nov 10, 2010 at 5:46 PM

Dear Marc,

It'd be great if I could use UpdateListItems with a CAMLQuery. Instead of supplying one ID at a time, I would like to update multiple items. Something like:

For each GetListItems (where: CAMLQuery), UpdateListItems (valuepairs: [["Status","Approved"]])

Assumption: this would be a GroupUpdate where every item would have the same update. Thoughts?

I know I can walk it out with a Get then Update each row in the completefunc, but it seems like it might be a common need. Or perhaps you'd just like me to write it up as a code sample for Update? :)

Jim Bob

Nov 10, 2010 at 9:31 PM
Edited Nov 10, 2010 at 9:32 PM

Alright, fine... here's a sample.

In this scenario, I have a list of Mileage Reimbursement records for the employee (dispUser). Each record has a Status ("Unsubmitted," "Pending," "Manager Approved," "Director Approved," "Paid," "Denied") and a Yes/No column named Promote.

submitPromotes() is called from a form action button on the page that passes in the current Status for the tab I'm on (six EasyTabs on the page—one for each Status, each with its own DVWP) and the "next" StatusGetListItems returns all records for the dispUser, where Status = fromStatus and Promote = true. Then, UpdateListItems goes through each one and updates them (by ID, of course) to Status = promoteTo and Promote = false.

For each stage in the approval process, the records enter the stage with Promote = false. When the user (Employee, Manager, Director, or Accounting) checks the Promote checkbox for the record, it doesn't get the new Status until s/he clicks the Submit Mileage button.

    function submitPromotes (fromStatus, promoteTo) {
        var qsUser = dispUser.items.EmployeeID;

        //Gather all of the records with Promote = True and Status = fromStatus
            operation: "GetListItems",
            async: false,
            listName: "Mileage Record",
            CAMLQuery: "<Query><Where>" +
                    "<And>" +
                        "<And>" +
                            "<Eq><FieldRef Name='EmpNo' /><Value Type='Text'>" + qsUser + "</Value></Eq>" +
                            "<Eq><FieldRef Name='Status' /><Value Type='Text'>" + fromStatus + "</Value></Eq>" +
                        "</And>" +
                        "<Eq><FieldRef Name='Promote' /><Value Type='Integer'>1</Value></Eq>" +
                    "</And>" +
            completefunc: function (xData, Status) {
                var rptCount = $(xData.responseXML).find("[nodeName=z:row]").length;
                if (rptCount == 0) {
                	alert("No mileage to submit");
                } else if (confirm("Are you sure you want to submit " + rptCount + " item(s)?")) {
                    $(xData.responseXML).find("[nodeName=z:row]").each(function() {
                        var thisID = $(this).attr("ows_ID");
                            operation: "UpdateListItems",
                            listName: "Mileage Record",
                            ID: thisID,
                            //Set Status = promoteTo; Set Promote = False
                            valuepairs: [["Status",promoteTo],["Promote",0]]
                        }); // end UpdateListItems
                    }); // end XML.each
                } // end else
            } // end completefunc
        }); // end GetListItems
    }; // end SubmitPromotes
Nov 10, 2010 at 9:34 PM

I knew if I waited a little while, you'd give me a good example.  ;+)

This could be the next great function in SPServices. How about SPUpdateListMultiple?


Nov 10, 2010 at 9:35 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Nov 10, 2010 at 9:35 PM

Oh yeah... and anOTHER thing:

I also use UpdateListItems when the user clicks the Promote checkbox so that I don't have to do a PostBack to the page.

    function togglePromote(thisID) {
        var ckVal = $("td#" + thisID + " input:checkbox:checked").length;
//    	alert(ckVal);
            operation: "UpdateListItems",
            listName: "Mileage Record",
            ID: thisID,
            //Set Promote = ckVal (0 if unchecked, 1 if checked)
            valuepairs: [["Promote",ckVal]]
        }); // end UpdateListItems
    }; // end togglePromote

So, there! Two samples for you. ;)