1

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);
clientContext.Load(documentLibrary);
clientContext.ExecuteQuery(); 

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);
clientContext.Load(newFile);
clientContext.ExecuteQuery();

I am using SharePoint.Client.dll of version 14.0.0.0 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.

Edits

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;
newFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();

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

4

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.

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.file.savebinarydirect(v=office.14)

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);
            }

        }

https://msdn.microsoft.com/en-us/pnp_articles/upload-large-files-sample-app-for-sharepoint?f=255&MSPPError=-2147217396

1

Follow these links and it will serve your purpose.

https://sharepointlessons.wordpress.com/2015/01/26/upload-large-files-using-csom-and-memory-streams/

http://www.stuartroberts.net/index.php/2014/05/09/sending-large-data-csom/

  • 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. – Trimantra Software Solution Oct 14 '16 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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