SharePoint 2016-CSOM-File Upload-via-ClientContext

February 27, 2018

Exception:
An unhandled exception of type 'System.Net.WebException' occurred in Microsoft.SharePoint.Client.dll Additional information: The remote server returned an error: (409) Conflict.

Code:
var clientContext = new ClientContext("http://myservername/sites/subsitename") { Credentials = LogonCredentials };
using (var fileStream = new FileStream(@"C:\Temp\Test.txt", FileMode.Open))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "/MyDocLibraryName/Test_FromClientOM.txt", fileStream, true);
}

Reason :
• Context can be from Root site or from Sub site, does not make difference.
• Verify that Second parameter to .SaveBinaryDirect( method is right.
• API does not create a folder
• Note: Second parameter is path starting from Root Site, Not Sub site.
So ideally code should be like:
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, “/sites/subsitename /MyDocLibraryName/MyFolderName/Test_FromClientOM.txt”, fileStream, true);

Enjoy SharePoint.

Advertisements