0

I managed to connect to out SharePoint instance with this code

using (var clientContext = new ClientContext(url))
{
    var oWebsite = clientContext.Web;
    clientContext.Load(oWebsite);
    clientContext.Credentials = new SharePointOnlineCredentials(username, password);

    clientContext.ExecuteQuery();

    Console.WriteLine("Title: {0} Created: {1}", oWebsite.Title, oWebsite.Created);
}

Which seemed to work one or two times, but normally when I run it I get this error on the ExecuteQuery() line:

The remote server returned an error: (429).

from this post I can see this error appears to be a "stop sending so many requests" warning, but is there way to stop this from happening? It's not like I'm doing anything which will put much strain on the server (simply connecting to SharePoint)

  • 1
    Somebody else might be doing something that's causing the throttling. – Derek Gusoff Feb 8 '18 at 15:34
  • Microsoft have changed something in SharePoint Online recently. It's possible to get the 429 errors even if you're not doing anything demanding. What worked for me was to add a "User Agent" to your connections/requests as documented in the "Avoid getting throttled or blocked..." link in Gautam's answer below. – mwardm Feb 27 '18 at 18:32
0

Not sure exactly why you are getting throttled (too many requests inside a for loop maybe ? ), you can use the ExecuteQueryRetry extension method of PnP CSOM.

This will try and attempt to execute the request after a specified interval (500ms).

So, modify your code as below:

using (var clientContext = new ClientContext(url))
{
    var oWebsite = clientContext.Web;
    clientContext.Load(oWebsite);
    context.AuthenticationMode = ClientAuthenticationMode.Default;
    clientContext.Credentials = new SharePointOnlineCredentials(username, password);

    clientContext.ExecuteQueryRetry();

    Console.WriteLine("Title: {0} Created: {1}", oWebsite.Title, oWebsite.Created);
}

To use PnP CSOM, go to your Project references -> manage nuget packages -> search for SharePointPnPCoreOnline and install it.

You can also take a look at the following GitHub sample to see how throttling has been handled.

GitHub sample - Core.Throttling

Excellent reference documentation - Avoid getting throttled or blocked in SharePoint Online

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.