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.