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.
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.
- Robert LuckFeb 13, 2018Iron ContributorIMO, encapsulating the 'Load' within the try-catch will not work. I think the context might be corrupted, You may reinitialize the context when the exception is thrown.
I guess you might be having a lot of calls to ExecuteQuery, I suggest you to try to reduce the calls by loading it them in single context and fire as a single call.
- Feb 12, 2018We have faced this issue last week and we noted that one of the possible issues was that the decoration for not being throttled was not correct...another thing you can take a look is to the possibility to make async queries to SPO...you can find more about this pattern in the PnP Community Call hold last week