Forum Discussion
SharePoint 429 Error - Throttling
Instead of 3 seconds delay between each query, you can execute one after the other. If you encounter 429 error, just inspect the response header for 'retry-after' entry. This retry-after field will contain the number of seconds you need to wait until you make your next query.
- Justin SpiteriFeb 12, 2018Copper Contributor
We already have tried this but unfortunately when there is a 429 error, and the ExecuteQuery retries, the returned information will contain missing fields. For example the File name of the item is returned as empty.
So unfortunately this was leading us to other issues and that's why we created a delay of 3 secs between each ExecuteQuery.
- Robert LuckFeb 12, 2018Iron Contributor
ExecuteQuery retries, the returned information will contain missing fields - This should not happen!. Can you share your retry code?
- Justin SpiteriFeb 13, 2018Copper Contributor
Hi Robert.. yes sure please find below:
for (var i = 0; i < 1000; i++)
{
var lib = ctx.Web.Lists.GetByTitle(libraryName);
var items = lib.GetItems(new CamlQuery());
ctx.Load(items);
try
{
ctx.ExecuteQuery();
}
catch(System.Net.WebException wex)
{
var response = wex.Response as System.Net.HttpWebResponse;
if (response != null && (response.StatusCode == (System.Net.HttpStatusCode)429 || response.StatusCode == (System.Net.HttpStatusCode)503))
{
var retryAfter = Int32.Parse(response.Headers["Retry-After"].ToString());
Thread.Sleep(TimeSpan.FromSeconds(retryAfter));
ctx.ExecuteQuery();
try
{
var x = items.Count; // throws CollectionNotInitializedException
}
catch(Exception ex2)
{
}
}
}
Console.WriteLine($"{i}:{items.Count}");
}
Some comments:
- I wrapping around the ExecuteQuery in a try/catch block and not the ctx.Load() because this suggested that should be enough: https://github.com/SharePoint/PnP/tree/dev/Samples/Core.Throttling
- My idea was to write an ExecuteQueryWithRetry code, similar to the one published by the PnP group but use your retry-after recommendation. This way, all I’d need to change within my code is replacing all calls to ExecuteQuery with ExecuteQueryWithRetry.
- The problem is that the we get an error when accessing the items collection after a retry attempt.
- I guess a “solution” could be to encapsulate the “Load” in the try catch block as well but while this is easy to accomplish in the above code, in our real life scenario it gets much more tricky as there would be a lot of lines of code to modify.
- Bryan SalibaFeb 12, 2018Copper ContributorThanks Robert,
Very interesting and we'll try it out.
Our main concern though is that we haven't been able to obtain a reply in 3 days if the site has been throttled and why, so as to resolve this and ensure it is not triggered again.