I am trying to upload a file with size 40MB in Document Library using SharePoint 2010 Managed CSOM (C#).

I am using following code

ClientContext clientContext = new ClientContext(siteUrl);
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(siteUrl), "NTLM", new NetworkCredential(userName, password));
clientContext.Credentials = myCache;
List documentLibrary = clientContext.Web.Lists.GetByTitle(documentLibraryName);

byte[] bFile = System.IO.File.ReadAllBytes(filePath);
FileCreationInformation file = new FileCreationInformation();
file.Content = bFile;
file.Url = url;
file.Overwrite = true;
Microsoft.SharePoint.Client.File newFile = documentLibrary.RootFolder.Files.Add(file);

I am using SharePoint.Client.dll of version so can't use methods provided for SP2013.

When I try to upload my file it throws me following exception

The remote server returned an error: (400) Bad Request

at System.Net.HttpWebRequest.GetResponse() at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute() at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb) at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery() at TestSampleApp.Program.uploadLargeData() in d:\RND\TestSampleApp\TestSampleApp\Program.cs:line 47 at TestSampleApp.Program.Main(String[] args) in d:\RND\TestSampleApp\TestSampleApp\Program.cs:line 16 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

I have gone through

All the setting specified in this links are already implemented. But still the error is being thrown.

Initially I was not able to upload the file using GUI too. But after making changes I am now able to upload 40MB file through UI. But not from CSOM (C#) code.

Please provide suggestion what I should configure additionally to get it resolved.


I also want to update the document property at the time of upload. Currently I am doing this with following code:

newFile.ListItemAllFields[internalNames-1] = values-1;
newFile.ListItemAllFields[internalNames-2] = values-2;

Is this possible with SaveBinaryDirect() in the same call?

2 Answers 2


you should be using some other method that bypasses the executQuery. I think its got a set timeout and max 2mb file limit.

use SaveBinaryDirect with filestream.


example function to call:

public void SaveBinaryDirect(ClientContext ctx, string libraryName, string filePath)
            Web web = ctx.Web;
            // Ensure that the target library exists. Create it if it is missing.
            if (!LibraryExists(ctx, web, libraryName))
                CreateLibrary(ctx, web, libraryName);

            using (FileStream fs = new FileStream(filePath, FileMode.Open))
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, string.Format("/{0}/{1}", libraryName, System.IO.Path.GetFileName(filePath)), fs, true);




Follow these links and it will serve your purpose.



  • 1
    Your second link leads to some positive solution. I am working on it. But Please include some of essential part of the code into your answer other wise it will be of no use if link get broken. Oct 14, 2016 at 8:00

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.