2

I'm using the Client Object Model to upload files from a network drive to a SharePoint document library. Here is how I would specify the upload location if I were uploading to the document library's root folder:

Microsoft.SharePoint.Client.File uploadFile = docLibrary.RootFolder.Files.Add(newFile);

How can I upload to a specific subfolder by name?

4

I think the simplest code is this:

ClientContext context = new ClientContext("http://MySiteUrl");
using (FileStream fs = new FileStream(@"x:\MyFile.docx", FileMode.Open))
{
   Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/MyLibName/MyFolderName/MyFile.docx", fs, true);
}

So you just need to provide sub-folder url.

References:

File.SaveBinaryDirect Method

Uploading files using Client Object Model in SharePoint 2010

ADDITION:

The The remote server returned an error: (409) Conflict. when running above code will occur if target folder doesn't exist.

To check if folder exist one can use:

ClientContext context = new ClientContext("http://MySiteUrl");
Folder f = web.GetFolderByServerRelativeUrl(listUrl + "/" + newDocSetName);
context .Load(f);
bool alreadyExists = false;
try
{
    clientContext.ExecuteQuery();
    alreadyExists = true;
}
catch { }

Code borrowed from Matt Rankin blog post: SharePoint 2010 : Using The Client Object Model To Move Files And Folders Across Site Collections And Subsites Check it out!

  • When I use FileStream, I get a (409) Conflict error. On the other hand, the first method suggested in your second link works to upload the file, but then I can't specify a subfolder. – LFurness Dec 9 '13 at 20:24
  • Target folder doesn't exist. You need to ensure that folder exist before you upload your file. I will update my answer. – Vedran Rasol Dec 9 '13 at 21:10
  • Target folder does exist, I'm pretty sure, checked several times. FileStream has a 409 error on my local machine, but works on the production server. So I will accept this answer. – LFurness Dec 9 '13 at 21:10
  • This works but the file remains checked-out. – user75190 May 25 '18 at 21:51
3

The accepted answer here may work but it is not proper user of the API. The two main problems with this kind of approach are (a) exceptions are being thrown around for common cases (e.g. a folder that may not exist, actually doesn't) and (b) you often won't be able to give fine grained explanations to the user about why the operation failed because most of the API is being bypassed.

The proper way to do what you're trying to do is the following:

var subFolders = docLibrary.RootFolder.Folders.Where(folder => folder.Name == "My Subfolder");

_context.ExecuteQuery();

var spFolder = subFolders.FirstOrDefault();

If spFolder is null, the sub folder does not exist (note: if you wanted to make a method for checking the existence of sub folders you could return subFolders.Any()). If it is not null then this is the folder and you can do all the same operations on it as you can on RootFolder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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