Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to save a file to an SP document library using the Client Object Model:

//populate information about the new file, from disk
FileCreationInformation fci = new FileCreationInformation();
fci.Url = fileName;
fci.Content = System.IO.File.ReadAllBytes(filePath);
fci.Overwrite = true;

//load the file collection for the documents in the library
FileCollection documentFiles = docSetFolder.Files;
clientContext.Load(documentFiles);
clientContext.ExecuteQuery();

//add this file to the file collection
Microsoft.SharePoint.Client.File newFile = documentFiles.Add(fci);
clientContext.Load(newFile);
clientContext.ExecuteQuery();

However, it turns out that by default there is a 1MB size limit when uploading files in this way. (Exception: 400 - Bad Request) This blog post details the options for getting around this. It turns out, though, that simply changing the file upload size as specified in the post won't work - the Microsoft.SharePoint library, which is required to do this, only runs on 64-bit CPUs. This is a reasonable assumption when writing server-side code, but this code is supposed to run on client machines. Seems like a pretty big oversight on Microsoft's part.

So I've got to use the second option.

using (ClientContext clientContext = new ClientContext("http://spsite/sites/TestSite/")) {

    using (FileStream fs = new FileStream(filePath, FileMode.Open))
    {
        string targetUrl = "testfile.txt";

        Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext,
            targetUrl,
            fs, true);                    
    }
}

When executing the call to SaveBinaryDirect, I get a 409 (Conflict) exception. What is the problem?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

The URL given to SaveBinaryDirect needs to be relative to the root site collection. So calling

    Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext,
        "/sites/TestSite/testfile.txt",
        fs, true); 

will work.

share|improve this answer
    
Thanks! ............. –  Eric Herlitz Feb 3 '12 at 20:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.