locked
"Bad Request Error (400)" while trying to upload files to SharePoint library RRS feed

  • Question

  • Hello,

    I was trying to Upload files to the SharePoint document library with Managed Object model.

    The clientContext.ExecuteQuery() throws an Error : "The remote server returned an error: (400) Bad Request."

    However i read few blogs saying that this may be an issue related to file size, but i verified the file i supposed to upload is only 200 KB in size. Also i don't have an access on the server so that i can't use the MaxReceivedMessageSize method to change the file size. 

    I am using the Add() for uploading the files to library..

    Can it be possible to resolve this error on client side only?

    Thanks in advance.

    Thursday, September 27, 2012 10:45 AM

Answers

All replies

  • Can you post your source code?

    Please "Mark as Answer" if a post has answered your question or "Vote as Helpful" if it was helpful in some way. Here's why

    Thursday, September 27, 2012 11:09 AM
  • Hi,

    Did you check this?

    http://stackoverflow.com/questions/6491696/maximum-file-upload-size-in-sharepoint

    Let us know your result


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Thursday, September 27, 2012 11:23 AM
  • Hi Nauzad,

    Thanks for reply,

    Here is the code m using to upload file to SharePoint library:

    ClientOM.ClientContex context = new ClientOM.ClientContext(urls);
    
    NetworkCredential nc = new NetworkCredential("username", "Password", "domain");
                            context.Credentials = nc;
    
     ClientOM.Web _web=context.Web;
                            context.Load(_web);
    
    string filepath = @"D:\SampleFile.TIF";
    
     ClientOM.FileCreationInformation zipInfo = new ClientOM.FileCreationInformation();
    
    zipInfo.Content = System.IO.File.ReadAllBytes(filepath);
    
    zipInfo.Url = "Path To Upload file";
    
     zipInfo.Overwrite = true;
    
    ClientOM.List zipDocumentLibrary = context.Web.Lists.GetByTitle("DocLibName");
    
    context.Load(zipDocumentLibrary);
    
    ClientOM.File zipFile = zipDocumentLibrary.RootFolder.Files.Add(zipInfo);
    
    context.Load(zipFile);
    
    context.ExecuteQuery();

    Thursday, September 27, 2012 11:27 AM
  • HI,

     Add below two lines in your code

    clientContext.Load(doclib)

    clientContext.Load(web)

    for your refernce check this link http://www.sharepointben.com/blog/Lists/Posts/Post.aspx?ID=612

    Thursday, September 27, 2012 11:30 AM
  • Hi Hemendra,

    Thanks for your reply.

    Yes I read that blog.

    When I tried to do using the SaveBinaryDirect method, I get an exception "Cannot invoke HTTP DAV request. There is a pending query." 


    Thursday, September 27, 2012 11:40 AM
  • Hi, 

    Thanks for reply,

    As you see i already added the Load methods in my source code..


    Thursday, September 27, 2012 11:42 AM
  • I copy pasted your code in my environment and it ran without any errors. So i am suspecting it is a file size related issue. Can you try using the SaveBinaryDirect method.  

    Refer to this link for more information - http://blogs.msdn.com/b/sridhara/archive/2010/03/12/uploading-files-using-client-object-model-in-sharepoint-2010.aspx


    Please "Mark as Answer" if a post has answered your question or "Vote as Helpful" if it was helpful in some way. Here's why

    Thursday, September 27, 2012 11:46 AM
  • Hi,

    Just try to convert stream to a byte array and used the FileCreationInformation object . Check this thread for more information about this:

    http://social.msdn.microsoft.com/Forums/en/sharepoint2010general/thread/cedbe646-2b58-4c53-a372-34bade0a39d6

    Hope it could help


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    • Proposed as answer by Nauzad Kapadia Thursday, September 27, 2012 1:42 PM
    • Marked as answer by Amogh Kulkarni Thursday, September 27, 2012 1:44 PM
    Thursday, September 27, 2012 12:00 PM
  • Hi Hemendra,

    Thanks for your reply,

    After conversion to byte array, the code is working fine..

    I just added 

      zipInfo.Content = new byte[] { };

    after

    zipInfo.Url = "Path To Upload file";

    in above code and it is working fine..

    Thanks. :-)

    • Marked as answer by Amogh Kulkarni Thursday, September 27, 2012 12:52 PM
    Thursday, September 27, 2012 12:52 PM
  • Hi All,

    I have a one folder in my local which has more than 20 Files. Total folder
    size is >10MB <11 MB, in those 17 files has <=300 KB and rest are
    >900KB <=1MB. I have to copy all the files to share point. I have used
    the same code as stated in the above, however my application starts copying
    files one by to share point it had copied 10 files successfully, it took 3 to 4
    min to copy 10 files later the application has thrown error "Bad Request
    Error (400)" while trying to upload files " Or "Time out
    expired". Is there any way to increase the file time out time from client
    side or any other way to copy the all the files to share point.<o:p></o:p>

    Thanks

    Mahesh

    Friday, February 14, 2014 10:28 AM