I have written a method that navigates through the subfolders of a folder in SharePoint. If the folder has many subfolders to go through, I get this error:

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.. in System.Net.HttpWebRequest.GetResponse() in Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute() in
Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)

If the folder doesn't have many subfolders in it , the method works fine.So, I was wondering that maybe there is something similar to KeepAlive property for SharePoint that I can use to prevent SharePoint from closing the connection.

I set the RequestTimeout property of clientContext to infinite, like suggested below, but I still get the error.

1 Answer 1


You probably looking for this:

ClientContext ctx = new ClientContext("http://server:port/sites/test");
ctx.RequestTimeout = System.Threading.Timeout.Infinite;
  • I tried setting the timeout to a very small value: ctx.RequestTimeout = 1; and I get this message now: The underlying connection was closed: An unexpected error occurred on a receive.
    – angie
    Jun 23, 2017 at 13:20
  • This is because you are adding time 1milisecond (1000ms = 1sec) of time, which is not enough to even make connections. For example put 60,000 as a value, then you'll get the error after 1min. Jun 23, 2017 at 13:35
  • Also, when we are unsure how much time is required to execute the following script, use Infinite. If this solves your problem please mark the ans as well. Jun 23, 2017 at 13:36
  • Hey, I set the timeout to infinite but I get the same error. Do you have any idea why?
    – angie
    Jul 3, 2017 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.