Recently I’ve been working on a solution that required using the client object model to upload large files to SharePoint. Not having a development background, I started searching for the best way to accomplish this task. I came across this helpful blog post that gave me a good start. I followed all the recommendations in the post and went with the HTTP DAV method mentioned second in the post.

However, I was uploading batches of files and getting a sporadic error stating:

System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. —> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. —> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

After hours searching and trying different attempts at fixing it without any progress I decide to switch back and use the client library batch mechanism mentioned first in the article. This worked much more consistently, but I was getting the “The remote server returned an error: (400) Bad Request.” That was mentioned. I figured it was the easy fix of increasing the message size, however, this didn’t fix my bad request error. Back to searching and I ran across this MSDN KB article about the issue. Come to find out there is are actually two lines of code not mentioned in the first link I referenced that were important to get the solution working.

  1. This one was mentioned further down in the comments, but it is to have clientContext.Load(doclib)
  2. This one was only in the second KB article and it was to have clientContext.Load(web)

After adding these two lines of code to my solution all files in the batch were consistently uploading via the object without any problems.

This may be common knowledge to some developers, but hopefully it can be helpful to some of you aspiring SharePoint developers out there.