0

I have two lists each containing around 4k records. First thing am doing is getting each record ID value from the first list and comparing it to the second list, if that particular ID exists in the second list, it should update a field 'Found' in first list to Yes. PFB my code. This is working perfectly fine but it is taking around 60 min to run. Is there any better way to do it. Thanks in advance.

<html>
<head>
<script type="text/javascript" src="/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/jquery.SPServices-0.5.6.min.js">    </script>
<script type="text/javascript">
$(document).ready(function()
{



var id='';
var found='';

            $().SPServices(
        {
        operation: "GetListItems",
        listName:"Test1",
               webURL:"http://test.com/sites/Test/          CAMLViewFields: '<ViewFields><FieldRef Name=\'ID\'/></ViewFields>',     
        async: false,
        completefunc: function (xData, Status) 
        {
            $(xData.responseXML).find("z\\:row, row").each(function() 
            {
            found=no;
            id=$(this).attr("ows_ID");


            $().SPServices(
            {
            operation:"GetListItems",
            listName:"Test2",
            webURL:"http://test.com/sites/Test/",
            CAMLQuery:"<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Number'>"+id+"</Value></Eq></Where></Query>",
            CAMLViewFields: '<ViewFields><FieldRef Name=\'ID\'/></ViewFields>',     
            async: false,
            completefunc: function (xData, Status) 
            {
            $(xData.responseXML).find("z\\:row, row").each(function() 
        {


             found=true;





            });
        }

        });


        $().SPServices({
         operation: "UpdateListItems",
         async: false,
         batchCmd: "Update",
         listName: "Test1",
         ID: id,
         valuepairs: [["Found",found]],
         completefunc: function (xData, Status) {

        }
                    });

        });
        }


});
alert("Done");
});
</script>
</head>
<body>

</body>
</html> 
  • I just need a better way to increase the performace and make the search faster – Dheeraj Feb 17 '17 at 4:06
1

Its taking 60 mins because you are making approx 4000 REST get calls and then another 4000 or less rest calls to update the list items.

So, you should first get the IDs of the Test 1 list, then get the IDs of Test 2 list. After that, you should find out the difference between these two lists' ID and then do a batch update of the different IDs.

For that , you can try below approach:

var firstListArray = new Array(), i=0;
var secondListArray = new Array(), j=0;

$().SPServices({
   operation: "GetListItems",
   listName: "Test1",
   webURL: "http://test.com/sites/Test/",
   async: false,
   CAMLQuery: x, //your caml query
   CAMLViewFields: '<ViewFields><FieldRef Name=\'ID\'/></ViewFields>',   
   completefunc: function(xData, ID) {
       $(xData.responseXML).find("z\\:row, row").each(function() {
           firstListArray[i++] = $(this).attr("ows_ID"); //store Ids of first list
       });
   }
});
console.log(firstListArray);


$().SPServices({
   operation: "GetListItems",
   listName: "Test2",
   webURL: "http://test.com/sites/Test/",
   async: false,
   CAMLQuery: x, //your caml query
   CAMLViewFields: '<ViewFields><FieldRef Name=\'ID\'/></ViewFields>',   
   completefunc: function(xData, ID) {
       $(xData.responseXML).find("z\\:row, row").each(function() {
           secondListArray[j++] = $(this).attr("ows_ID"); //store Ids of second list
       });
   }
});

console.log(secondListArray);

var diff = $(firstListArray).not(secondListArray).get(); //get the difference between two list ids


var update_string="<Batch OnError='Continue'>";
var count=0;

for(var i=0;i<diff.length;i++)  // diff is having different list items IDs
{  
    count++;
    update_string = update_string+"<Method ID='"+count+"' Cmd='Update'>" +
        "<Field Name='ID'>"+diff[i]+"</Field>" +
        "<Field Name='Found'>1</Field>" +
        "</Method>";  
} 
update_string = update_string+"</Batch>";

$().SPServices({
  operation: "UpdateListItems",
  async: true,
  batchCmd: "Update",
  listName: "Test1",
  updates: update_string,
  completefunc: function(xData, Status)
  {
    console.log("success");     
    console.log(xData.responseText);     
  }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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