There are several options for specifying a subfolder when downloading a file using CSOM
There are two assumptions about the solutions provided below:
Using the FileCreationInformation.Url
Use the FileCreationInformation.Url property to specify the folder URL for the downloaded file.
The following example shows how to specify a relative url (a slightly modified version of your example, the main difference is the definition of FileCreationInformation.Url )
var uploadFilePath = @"c:\tmp\SharePoint User Guide.docx"; var fileCreationInfo = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(uploadFilePath), Overwrite = true, Url = Path.Combine("Documents/Folder/Sub Folder/Sub Sub Folder/Sub Sub Sub Folder/", Path.GetFileName(uploadFilePath)) }; var list = context.Web.Lists.GetByTitle("Root Folder"); var uploadFile = list.RootFolder.Files.Add(fileCreationInfo); context.Load(uploadFile); context.ExecuteQuery();
Using the Web.GetFolderByServerRelativeUrl
Use the Web.GetFolderByServerRelativeUrl method to retrieve the folder where the file should be loaded:
public static void UploadFile(ClientContext context,string uploadFolderUrl, string uploadFilePath) { var fileCreationInfo = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(uploadFilePath), Overwrite = true, Url = Path.GetFileName(uploadFilePath) }; var targetFolder = context.Web.GetFolderByServerRelativeUrl(uploadFolderUrl); var uploadFile = targetFolder.Files.Add(fileCreationInfo); context.Load(uploadFile); context.ExecuteQuery(); }
Using
using (var ctx = new ClientContext(webUri)) { ctx.Credentials = credentials; UploadFile(ctx,"Documents/Folder/Sub Folder/Sub Sub Folder/Sub Sub Sub Folder",filePath); }
source share