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
- How to upload large file(s) in SharePoint?
- Issues Uploading Large Files To SharePoint
- SharePoint 2010: HowTo upload a file of the size of 1 GB using client object model
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?