Timeout error during file upload in document library using managed client object model : Microsoft.SharePoint.Client.File.SaveBinaryDirect
There are mainly 2 ways to upload files in sharepoint document library using client object model. One way is to use Microsoft.SharePoint.Client.File.SaveBinaryDirect method and other one is to use documentLibraryObject.RootFolder.Files.Add(newFileCreationInformation) method. documentLibraryObject.RootFolder.Files.Add method increases the message size because of BASE64 encoding.
While uploading large files, Microsoft.SharePoint.Client.File.SaveBinaryDirect can be used, in order to avoid increased message size. While doing so, there is possibility that you may get exception saying "The underlying connection was closed: A connection that was expected to maintain their closed down the server". The reason for this is sometimes WebRequest gets time out while uploading the large file.
To solve this problem, set the time out of the context before calling the Microsoft.SharePoint.Client.File.SaveBinaryDirect method.
context.RequestTimeout = 3600000; // Time in milliseconds
Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, relativePath, filestream, overWrite);.
Reason behind setting context.RequestTimeout is, internally while creating the web request object, value of context.RequestTimeout will be copies to webRequest.Timeout.
Comments
Anonymous
May 17, 2011
Can we specify this "Sessiontimeout" in Web.config file?Anonymous
May 17, 2011
Since Microsoft.SharePoint.Client.File.SaveBinaryDirect uploads file using webrequest and time out property of this webrequest takes the value of context.RequestTimeout hence values in web.config files won't help. We need to set the context.RequestTimeout with appropriate value in order to set webReqesut.timeout.Anonymous
May 15, 2012
Hi, Even i give the RequestTimeout, i am geting the same error.Anonymous
May 15, 2012
I can able to upload the files without any issues in my server machine. While running the exe in different server i am getting underlying connect was closed error. I hav added RequestTimeout but not working. Can you pls help to resolve this?Anonymous
May 17, 2012
Reason you are getting this issue on different server is because of network latency causing time out. What was the time out value you tried.Anonymous
May 17, 2012
The time out i set to 3600000. Do i need to do any other changes in IIS to make the keep alive status?Anonymous
June 20, 2012
You can try increasing Connection time-out in IIS.Anonymous
August 15, 2013
Kaushalendra, Do you have any experience using SharePoint 2013 in Office 365 (SharePoint.com)? Getting system settings from Microsoft is not easy. Microsoft says "in some posts" that the Limit for SharePoint Online is 250MB file size and 20 minutes. They also say that if you have not uploaded the file on 20 minutes it will timeout (which makes sense). Question 1) Does SaveBinaryDirect use the 180 second requesttimeout, or is that only for myContext.ExecuteQuery? Question 2) can you think of any way to programmatically find the server settings (250 MB, and 20 minutes). Its better than an obscure post by a Microsoft Support person who may not have known what he was talking about. Cheers SavinAnonymous
May 08, 2014
For this code I basically increased the Timeout to Timeout.Infinite but still get the error below: Error: System.Net.WebException: The remote server returned an error: (504) Gateway Timeout. at System.Net.HttpWebRequest.GetResponse() Any suggestions are welcome.Anonymous
October 20, 2015
I have encountered this problem with Microsoft.SharePoint.Client.File.SaveBinaryDirect when uploading a number of big files in a row. The fix for this, is on each time you are using SaveBinaryDirect, wrap it with a try-catch, and catch System.Net.WebException. once Webexception is caught, run the same SaveBinaryDirect command you ran before, and it should work.