15 December 2011

Sharepoint Upload File Error: ‘The remote server returned an error: (409) Conflict’

I’ve been using the following code to upload a file to a Sharepoint document library using the Client Object Model:

Code Snippet
  1. public string Save(string fileName, Stream fileStream)
  2.         {
  3.             string destinationUrl = String.Concat(serverRelativeLibraryUrl, "/", fileName);
  4.             ClientContext clientContext = new ClientContext(webUrl);
  5.             {
  6.                 Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, destinationUrl, fileStream, true);
  7.             }
  9.             return destinationUrl;
  10.         }

serverRelativeLibraryUrl = String.Concat(webRelativeUrl, libraryName);

But I kept getting the 409 Conflict Error:
ExceptionType: 'WebException'  ExceptionMessage: 'The remote server returned an error: (409) Conflict.'   StackTrace: ' 
at System.Net.HttpWebRequest.GetResponse()     
at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()     
at Microsoft.SharePoint.Client.File.SaveBinary(ClientContext context, String serverRelativeUrl, Stream stream, String etag, Boolean overwriteIfExists, SaveBinaryCheckMode checkMode)     
at Microsoft.SharePoint.Client.File.SaveBinaryDirect(ClientContext context, String serverRelativeUrl, Stream stream, Boolean overwriteIfExists)     

After struggling for a while, I figured out that the problem was the library name. It contained a dash on it, like “My-LibraryName”. When I renamed it without the dash it started working.

The error in the code is how I built the URL. I was using the library name to build the library URL, but it seems that Sharepoint doesn’t like some characters in the URL, so when you create a library called “My-LibraryName”, the URL is actually http://<server>/MyLibraryName, without the dash.

Technorati Tags: