Wednesday, 20 March 2019

Handling throttling limit in SharePoint CSOM using PNP

Source-Img :Internet

SharePoint Online uses throttling to maintain optimal performance and reliability of the SharePoint Online service. Throttling limits the number of user actions or concurrent calls (by script or code) to prevent overuse of resources.
That said, it is extremely rare for a user to get throttled in SharePoint Online. The service is robust, and it is designed to handle very high volume. If you do get throttled, 99% of the time it is because of custom code. That doesn't mean that there aren't other ways to get throttled, just that they are less common.

 For example you spin up 10 machines and have a sync client going on all 10. On each sync 1TB of content. This would likely get you throttled.

This sample shows pattern on how to handle possible SharePoint Online throttling which could be applied for CSOM, REST and web service operations in the SharePoint Online.

static void Main(string[] args)
    string serverUrl = "";
    String login = "Demo\Gowthamm";
    String password = "******">";
    string listUrlName = "DemoDocuments";

    using (var ctx = new ClientContext(serverUrl))
        //Provide account and pwd for connecting to the source
        var passWord = new SecureString();
        foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
        ctx.Credentials = new SharePointOnlineCredentials(login, passWord);

            int number = 0;
            // This loop will be executed 1000 times, which will cause throttling to occur
            while (number < 1000)
                // Let's try to create new folder based on Ticks to the given list as an example process
                var folder = ctx.Site.RootWeb.GetFolderByServerRelativeUrl(listUrlName);
                folder = folder.Folders.Add(DateTime.Now.Ticks.ToString());
                // Extension method for executing query with throttling checks
                ctx.ExecuteQueryWithIncrementalRetry(5, 30000); //5 retries, with a base delay of 10 secs.
                // Status indication for execution.
                Console.WriteLine("CSOM request successful.");
                // For loop handling.
                number = number + 1;
        catch (MaximumRetryAttemptedException mex)
            // Exception handling for the Maximum Retry Attempted


Author: verified_user