title: How to search-and-replace list item names using SPServices and SharePoint 2010?
body: <p>My goal is to modify list items (including display names, filenames, titles, URLs, etc) throughout our SharePoint site.  This is needed due to a major business reorganization which has altered the business hierarchy and project names.  We have way too many files and URLs to edit them manually.  We need to perform the equivalent of a search-and-replace operation on all list items throughout the SharePoint site.  So far, the code examples we've discovered for updating list items have all been simple set-a-flag styles of operations using the same values for all list items, but we need to read the contents of each list item and use those values as part of the newly-updated values (unless SPServices/SharePoint has some equivalent of Unix grep/sed commands).</p>
  
  <p>For development and testing, I have added the following HTML/javascript code into a Content Editor Web Part (CEWP)'s HTML section which is located on my MySite's Contents page (because I really do not want to experiment against live production data), and this MySite also includes lots of text/DOC/PPT/XLS files with various names/titles/etc to search through.  For now, this script attempts to modify a single file named "test4 blah.txt" (eventually I will replace this CAMLQuery to modify multiple sets of files).  The javascript executes without errors, successfully finds the queried-for list items, and displays the supposedly-modified list items in the GUI with the supposedly-modified new values (i.e. everything looks successful from the user's point of view of the GUI), but unfortunately behind the scenes, the actual list items in SharePoint are never actually modified.  No error messages were printed into the javascript console when this script executes.  In the code's UpdateListItems section, we have tried lots of variations on internal static-names, display-names, etc with no success.</p>
  
  <p>We are using Microsoft SharePoint 2010, SPServices (jquery.SPServices-2014.01.min.js), and jQuery (jquery-1.11.0.min.js).  I have full Site Collection Administrator privileges. </p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;script type="text/javascript" src="https://MySiteName123/SiteAssets/jquery.min.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript" src="https://MySiteName123/SiteAssets/jquery.SPServices.min.js"&gt;&lt;/script&gt;
  
          &lt;title&gt;Edit the file&lt;/title&gt;
          &lt;meta charset="UTF-8"&gt;
          &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  
          &lt;style type="text/css"&gt;
              .btn {
                  font-family: verdana;
                  font-weight: normal;
                  cursor: pointer;
                  color: #ffffff;
                  font-size: 16px;
                  background: #3498db;
                  padding: 10px 20px 10px 20px;
                  text-decoration: none;
                  }
              .btn:hover {
                  background: #3cb0fd;
                  text-decoration: none;
                  }
          &lt;/style&gt;        
  
          &lt;script type="text/javascript"&gt;
              // Define some of our variables as "global" (to be accessible from our jQuery external libraries).
              var modifiedListItems = 0;
              var modifiedListItemsAttempted = 0;
              var ms2min = 1 / (1000 * 60);  // constant to convert milliseconds into minutes
  
              function EditTheFile() {
                  myStartDate = new Date();
                  $("#ScriptStatus").show();
                  $('#ScriptStatus').append("Started running at " + myStartDate + "&lt;br&gt;");
  
                  // Loop through all sites in the site collection
                  GetAllSites();
  
                  myEndDate = new Date();
                  myDuration = (myEndDate - myStartDate) * ms2min;  // date differences are calculated in milliseconds    
                  $('#ScriptStatus').append("Finished running at " + myEndDate + "; duration was " + (Math.round(myDuration * 100) / 100) + " minutes" + "&lt;br&gt;");
                  $('#ScriptStatus').append("Number of modified List Items = " + modifiedListItems + " out of " + modifiedListItemsAttempted + " attempts" + "&lt;br&gt;");
              }    
  
              function GetAllSites() {
                 $().SPServices({
                      operation: "GetAllSubWebCollection",
                      async:false,
                      completefunc: function (xData, status) {
                          $(xData.responseXML).find("Webs &gt; Web").each(function() {           
                              mySiteTitle = $(this).attr("Title");
                              mySiteURL = $(this).attr("Url");
                              GetAllLists(mySiteURL);
                          });
                      }
                  });
              }  // end of GetAllSites      
  
              function GetAllLists(mySite) {
                  $().SPServices({
                      operation: "GetListCollection",
                      webURL: mySite,
                      async: false,
                      completefunc: function (xData, status) {
                          $(xData.responseXML).find("List").each(function() {
                              myListTitle = $(this).attr("Title");
                              myListURL = $(this).attr("DefaultViewUrl");
                              myListType = GetListType( $(this).attr("BaseType") );           
                              ModifyListItemURL(mySiteURL, myListURL, myListTitle); 
                          }); 
                      }  
                  }); 
              }  // end of GetAllLists            
  
              function ModifyListItemURL(myURL, myListURL, myListName) {
                  $().SPServices({
                      operation: "GetListItems",
                      webURL: myURL,  
                      listName: myListName,  
                      async: false,
                      CAMLQuery: "&lt;Query&gt;&lt;Where&gt;&lt;Contains&gt;&lt;FieldRef Name='FileLeafRef' /&gt;&lt;Value Type='Text'&gt;test4 blah&lt;/Value&gt;&lt;/Contains&gt;&lt;/Where&gt;&lt;/Query&gt;",
                      CAMLViewFields: '&lt;ViewFields Properties="True"/&gt;',
                      completefunc: function(xData, Status) {
                          $(xData.responseXML).SPFilterNode('z:row').each(function() {
                              var listItemID = $(this).attr("ows_ID");
                              var myListItemName = $(this).attr("ows_FileLeafRef");  // SharePoint display-name of "Name" is equivalent to internal-name of "FileLeafRef" 
                              var myListItemURL = $(this).attr("ows_EncodedAbsUrl");  // SharePoint display-name of "Encoded Absolute URL" is equivalent to internal-name of "EncodedAbsUrl" 
                              var myListItemTitle = $(this).attr("ows_Title");
  
                              // Modify the local variable's text values.
                              var new_myListItemName = myListItemName.replace("blah", "doubleplus-blah");
                              var new_myListItemURL = myListItemURL.replace("blah", "doubleplus-blah");
                              console.log("stevie17 modifyTheData for Name, oldString=" + myListItemName + ", newString=" + new_myListItemName);
                              console.log("stevie18 modifyTheData for EncodedAbsUrl, oldString=" + myListItemURL + ", newString=" + new_myListItemURL);
                              myListItemName = new_myListItemName;
                              myListItemURL = new_myListItemURL;
  
                              // Modify the data to have the newly-modified text values.
                              $().SPServices({
                                  operation: 'UpdateListItems',
  //                                webURL: myURL,    
                                  listName: myListName, 
                                  async: false,
                                  updates: '&lt;Batch&gt;&lt;Method ID="1" Cmd="Update"&gt;'
                                      + '&lt;Field Name="ID"&gt;' + listItemID + '&lt;/Field&gt;'
  //                                    + '&lt;Field Name="FileLeafRef"&gt;' + myListItemName + '&lt;/Field&gt;'
  //                                    + '&lt;Field Name="EncodedAbsUrl"&gt;' + myListItemURL + '&lt;/Field&gt;'
  //                                    + '&lt;Field Name="Name"&gt;' + myListItemName + '&lt;/Field&gt;'
  //                                    + '&lt;Field Name="Encoded Absolute URL"&gt;' + myListItemURL + '&lt;/Field&gt;'
                                      + '&lt;Field Name="ows_FileLeafRef"&gt;' + myListItemName + '&lt;/Field&gt;'
                                      + '&lt;Field Name="ows_EncodedAbsUrl"&gt;' + myListItemURL + '&lt;/Field&gt;'
                                      + '&lt;/Method&gt;&lt;/Batch&gt;',
                                  completefunc: function(xData, Status) {
                                      modifiedListItemsAttempted++;
  
                                      // Append information to the display table.
                                      $('#ListInfoTable &gt; tbody:last').append("" +
                                          "&lt;tr&gt;" +                                                    // Start row
                                          "&lt;td&gt;" + mySiteURL + "&lt;/td&gt;" +                                  // Site URL
                                          "&lt;td&gt;" + mySiteTitle + "&lt;/td&gt;" +                    // Site Title   
                                          "&lt;td&gt;" + myListName + "&lt;/td&gt;" +                         // List Title          
                                          "&lt;td&gt;" + myListURL + "&lt;/td&gt;" +                                  // List URL
                                          "&lt;td&gt;" + myListType + "&lt;/td&gt;" +                                 // List Type                                        
                                          "&lt;td&gt;" + myListItemURL + "&lt;/td&gt;" +                              // List Item URL                                        
                                          "&lt;td&gt;" + myListItemName + "&lt;/td&gt;" +             // List Item Name
                                          "&lt;td&gt;" + myListItemTitle + "&lt;/td&gt;" +                            // List Item Title
                                          "&lt;/tr&gt;" + 
                                          "");                            
  
                                      if (Status != "success") {
                                          alert("Something went wrong with the update procedure.");
                                      }
                                      else {
                                          modifiedListItems++;
                                      }
                                  }
                              });             
                          });
                      }
                  });
              }  // end of GetListItemURL
  
              // Display a human-readable form for the item type.
              function GetListType(myBaseType) {
                  var myBaseTypeDescription;
                  if       ( myBaseType == 0 ) { myBaseTypeDescription = "Generic List"; }
                  else if  ( myBaseType == 1 ) { myBaseTypeDescription = "Document Library"; }
                  else if  ( myBaseType == 2 ) { myBaseTypeDescription = "Unused"; }
                  else if  ( myBaseType == 3 ) { myBaseTypeDescription = "Discussion Board"; }
                  else if  ( myBaseType == 4 ) { myBaseTypeDescription = "Survey"; }
                  else if  ( myBaseType == 5 ) { myBaseTypeDescription = "Issue"; }
                  else                         { myBaseTypeDescription = "None"; }
  
                  return myBaseTypeDescription;
              }  // end of GetListType            
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;!-- Display GUI controls for the query operations. --&gt;
          &lt;div&gt;
              &lt;span class="btn" style="width:100px; text-align:center; margin-top:5px; margin-bottom:10px; display:inline-block" onClick="javascript:EditTheFile();"&gt;Edit the file&lt;/span&gt;
          &lt;/div&gt;
  
          &lt;!-- Display summary statistics about the query results. --&gt;
          &lt;div id="ScriptStatus" style="padding:5px; margin-bottom:10px; border:thin gray solid; display:none;"&gt;
          &lt;/div&gt;
  
          &lt;!-- Display a table with the query results. --&gt;
          &lt;table id="ListInfoTable" cellpadding="2" cellspacing="2" border="1"&gt;
              &lt;thead&gt;
                  &lt;tr bgcolor="#E4E4E4"&gt;
                      &lt;th&gt;Site URL&lt;/th&gt;
                      &lt;th&gt;Site Title&lt;/th&gt;
                      &lt;th&gt;List Title&lt;/th&gt;
                      &lt;th&gt;List URL&lt;/th&gt;
                      &lt;th&gt;List Type&lt;/th&gt;
                      &lt;th&gt;ListItem URL&lt;/th&gt;
                      &lt;th&gt;ListItem Name&lt;/th&gt;
                      &lt;th&gt;ListItem Title&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;        
              &lt;tbody&gt;
              &lt;/tbody&gt;
          &lt;/table&gt;        
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>