reading and writing list data in SharePoint 2010 using SPServices


/ Published in: jQuery
Save to your folder(s)

Just syntax examples here. First block reads, second writes. Top function just a debugging helper.

Using jquery 1.5, $(xData.responseXML).find("nodeName=z:row]").each(function(i)... may need to be written as
$(xData.responseXML).find("nodeName='z:row']").each(function(i)... with single quotes around 'z:row'.


Copy this code and paste it in your HTML
  1. function printObject(o){
  2. var output = '';
  3. for (property in o) {
  4. output += property + ': ' + o[property]+'; ';
  5. }
  6. alert(output);
  7. }
  8.  
  9.  
  10.  
  11.  
  12. function readThenWrite(){
  13.  
  14.  
  15.  
  16. $().SPServices({
  17. operation: "GetListItems",
  18. webURL: "/sitepath", /*optional parameter used if list lives in parent site */
  19. async: false,
  20. listName: "Badge Achievements", /*name of the list */
  21. viewName: '{D6ED584C-4673-4B89-B539-FF43D936ABC9}',/*optional to specify view */
  22. CAMLRowLimit: 4, /*optional to limit results */
  23. CAMLQuery: '<Query><Where><Eq><FieldRef Name="Author" /><Value Type="User">' + userName + '</Value></Eq></Where></Query>',
  24. completefunc: function (xData, Status) {
  25. //printObject(xData);
  26. $(xData.responseXML).find("[nodeName=z:row]").each(function(i) {
  27. arrBadgesAchvd[i] = $(this).attr('ows_Badge').split("#")[1];
  28. });
  29. }
  30.  
  31. });/*-End SpServices*/
  32.  
  33.  
  34.  
  35.  
  36. $().SPServices({
  37. operation: "UpdateListItems",
  38. async: false,
  39. listName: "Learning Path Completion",
  40. updates: "<Batch OnError='Continue'>" +
  41. "<Method ID='1' Cmd='Update'>" +
  42. "<Field Name='Title'>percDone</Field>" +
  43. "<Field Name='User'>" + userID + "</Field>" +
  44. "<Field Name='Completed'>" + lpdone + "</Field>" +
  45. "<Field Name='Total'>" + lptotal + "</Field>" +
  46. "<Field Name='ID'>" + docid + "</Field>" +
  47. "</Method>" +
  48. "</Batch><Where></Where>",
  49. completefunc: function(xData, Status) {}
  50. });//End SPSErvices
  51.  
  52.  
  53.  
  54.  
  55. }/*End readThenWrite*/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.