Wednesday, October 6, 2010

SharePoint 2010 Client Object Model: "(400) Bad Request" error

If you use Microsoft.SharePoint.Client.FileCollection.Add or Microsoft.SharePoint.Client.File.SaveBinary
method to create (or change) file in SharePoint 2010, you can get "The remote server returned an error: (400) Bad Request" error.

To resolve this issue you need to change default Maximum Message Size for WCF calls:

open SharePoint 2010 Management Shell
type:
$ws = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$ws.ClientRequestServiceSettings.MaxReceivedMessageSize = your_value
$ws.Update()

I recommend to set MaxReceivedMessageSize to Int32.MaxValue - 1 (2147483646).

Sometimes you may need to run "iisreset /noforce" to enforce changes applying.

Also you can use Microsoft.SharePoint.Client.File.SaveBinaryDirect approach that does not have this limitation.

5 comments:

  1. Well Does FileCollection.Add is useful uploading file to sharepoint online? 
    As I got error related as described above?  i dont think we are allowed to change value as you have described above. Is there any solution in case of sharepoint online? 

    ReplyDelete
  2. Very very helpful thank you! Worked a treat!

    ReplyDelete
  3. Thank You So Much..
    really working like a charm

    ReplyDelete

Note: Only a member of this blog may post a comment.