Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:57:48+00:00 2026-06-04T12:57:48+00:00

I created a recursive loop to delete items in a SharePoint list with SPServices.

  • 0

I created a recursive loop to delete items in a SharePoint list with SPServices. I put a timeout in for 5 seconds; however for some reason it only deletes 2 items at a time, and then executes the wait instead of deleting the batch, and then repeating the function.

Please find my code below:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $intStart = 1;
        $intCount = 10;
        $intEnd = 40;

        $("input[class='buttonpush']").click(function () {
            var looper = function () {
                if ($intStart < $intCount && $intStart < $intEnd) {
                    $().SPServices.SPUpdateMultipleListItems({
                        webURL: "http://widgettest",
                        listName: "TestList",
                        CAMLQuery: "<Query><Where><And><Gt><FieldRef Name='ID' /><Value Type='Counter'>" + $intStart + "</Value></Gt><Lt><FieldRef Name='ID' /><Value Type='Counter'>" + $intCount + "</Value></Lt></And></Where></Query>",
                        batchCmd: "Delete",
                        valuepairs: [],
                        debug: false,
                        completefunc: function (xData, Status) {
                            $intStart = $intStart + 5;
                            $intCount = $intCount + 5
                            setTimeout(looper, 5000);
                        }
                    });
                }
                else { 
                }
            }
            looper();
        });
    });

</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:57:50+00:00Added an answer on June 4, 2026 at 12:57 pm

    OK, now that you show the initial call to looper(), there does not appear to be anything wrong with the logic. looper() should run 8 times until $intStart is equal to 41. These are the reasons that you might not get as many successive runs of looper() as you are expecting:

    1. You get an error from $().SPServices.SPUpdateMultipleListItems() and completeFunc is not executed like you are expecting.
    2. Something else in your code is manipulating the values of the global variables $intStart, $intCount or $intEnd so they get changed accidentally while you are running and thus the loop doesn’t run as many iterations as expected.
    3. You get a javascript exception somewhere during the running of looper() that causes execution to stop.

    To debug this, just put a unique console.log() statement right before the call to $().SPServices.SPUpdateMultipleListItems() and another one in the completeFunc so you can tell exactly how far it’s getting before stopping.

    Some other observations on the code:

    1. $intStart is always less than $intCount since it starts less and both are incremented by 5 so you don’t need that part of the if statement.
    2. It is not customary to preceded javascript variables with a $ sign unless you are connoting some special kind of variable. That is not the case here and just impedes the readability of the javascript to someone familiar with javascript.
    3. Your variables $intStart, $intCount or $intEnd should probably not be global variables and in all cases should not be implicitly declared or accidentally declared globals. If you mean for them to be globals, then declare them as globals in the global scope using var. If you mean for them to be local to some scope, then declare them in that scope.
    4. Because your variables are global and are only initialized one at document.ready() time, your button push will only work the first time. It will never do anything when pushed a second time.
    5. Your code is not protected from the user pushing the button again while looper() is running (this will cause duplicate requests to be launched).

    Here’s what your CAMLQuery will be the first two times:

    <Query>
        <Where>
            <And>
                <Gt>
                    <FieldRef Name='ID' />
                    <Value Type='Counter'>1</Value>
                </Gt>
                <Lt>
                    <FieldRef Name='ID' />
                    <Value Type='Counter'>10</Value>
                </Lt>
            </And>
        </Where>
    </Query>"    
    
    <Query>
        <Where>
            <And>
                <Gt>
                    <FieldRef Name='ID' />
                    <Value Type='Counter'>6</Value>
                </Gt>
                <Lt>
                    <FieldRef Name='ID' />
                    <Value Type='Counter'>15</Value>
                </Lt>
            </And>
        </Where>
    </Query>"    
    

    This doesn’t look correct to me. The first time, you’re asking to process > 1 && < 10. The second time, you’re asking to process > 6 && < 15.

    Shouldn’t the second time be > 9 && < 18 to get new ones that weren’t already processed?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a concurrent, recursive directory traversal and file processing program, which sometimes
Does anybody know how I could create a recursive loop with NAnt? I need
I'm trying to create a recursive function in coldfusion and am coming across some
I have a multi-thread C# application that uses some recursive functions in a dll.
The problem: To create a recursive array function that walks through a three dimensional
Is is possible to create a recursive route in Rails? I have an application,
Is it possible to create a LL(1) table-driven (non-recursive) compiler with ANTLR or ANTLR3
Lets say I have a recursive function that creates lists within lists. It return
Created .NET WCF service, tested it - works. Generated schemas from Data and service
Created Private Key & Self signed certficate in a Key Store keytool -genkey -alias

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.