Expand Minimize

SPC059001: Consider implementing a pattern to avoid throttling with ClientContext.ExecuteQuery()

SharePoint uses throttling to maintain optimal performance and reliability. Throttling limits the number of user actions or concurrent calls (by script or code) to prevent overuse of resources. Make sure you implement a pattern to retry your code execution in case you should get throttled.

CheckId SPC059001
TypeName AvoidBeingThrottledInCSOM
Severity Warning
Type Assembly

Bad Practice

using (var context = new ClientContext("http://yoursite"))
{
    // While this isn't necessarily a bad practice, it is important to understand the throttling limits and that you may want to avoid it.
    // In this example, we don't handle retries or consider that throttling can kill our request.
    context.ExecuteQuery();
}

Good Practice
using (var context = new ClientContext("http://yoursite"))
{
    // While this isn't necessarily a bad practice, it is important to understand the throttling limits and that you may want to avoid it.
  // In this example we will do 5 retries with 500ms intervals
    context.ExecuteQueryWithIncrementalRetry(context, 5, 500);
}

// A helper method to handle incremental query retry on the ExecuteQuery method.
public static void ExecuteQueryWithIncrementalRetry(this ClientContext context, int retryCount, int delay)
{
    int retryAttempts = 0;
    int backoffInterval = delay;
    if (retryCount <= 0)
        throw new ArgumentException("Provide a retry count greater than zero.");
    if (delay <= 0)
        throw new ArgumentException("Provide a delay greater than zero.");
    while (retryAttempts < retryCount)
    {
        try
        {
            context.ExecuteQuery();
            return;
        }
        catch (WebException wex)
        {
            var response = wex.Response as HttpWebResponse;
            if (response != null && response.StatusCode == (HttpStatusCode)429)
            {
                //Add delay.
                System.Threading.Thread.Sleep(backoffInterval);
                //Add to retry count and increase delay.
                retryAttempts++;
                backoffInterval = backoffInterval * 2;
            }
            else
            {
                throw;
            }
        }
    }
    throw new MaximumRetryAttemptedException(string.Format("Maximum retry attempts {0}, have been attempted.", retryCount));
}

To suppress this violation in managed code add the following attribute to the method which contains the instruction (available since SPCAF version v5.2). Learn more about SuppressMessage here.

// Important: Ensure to have #define CODE_ANALYSIS at the beginning of your .cs file
[SuppressMessage("SPCAF.Rules.ManagedCSOM.CSOMBestPracticesGroup", "SPC059001:AvoidBeingThrottledInCSOM", Justification = "Provide reason for suppression here")]
Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of Rencore. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.