CSOM - Upload a file to a subdirectory in Sharepoint Online

developer sp1 461 Reputation points
2020-10-15T03:17:30.397+00:00

var uploadFile = list
.RootFolder
.Folders
.GetByPath(ResourcePath.FromDecodedUrl("A/B/C"))
.Files
.Add(newFile);

ctx.Load(uploadFile);
await ctx.ExecuteQueryAsync();

Throws System.IO.DirectoryNotFoundException even though both directories exist. So how can I upload a file to the subdirectory "C" under A/B ?

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,630 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jerryzy 10,571 Reputation points
    2020-10-15T07:56:25.357+00:00

    Hi @developer sp1 ,

    Try to upload file into sub folder like this:

    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 (var ctx = new ClientContext(webUri))  
    {  
         ctx.Credentials = credentials;  
      
         UploadFile(ctx,"LibName/FolderName/Sub Folder Name/Sub Sub Folder Name/Sub Sub Sub Folder Name",filePath);     
    }  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Nelson Soares 0 Reputation points
    2025-01-22T20:32:45.2866667+00:00

    Hi,  I´m using something like this:  

    string userName = "nelson.soares@.pt"; string password = ""; var securePassword = new SecureString();  / foreach (char c in password) {  securePassword.AppendChar(c); }  using (var clientContext = new ClientContext("https://.sharepoint.com/sites/***/")) {  clientContext.Credentials = new NetworkCredential(userName, securePassword);  Web web = clientContext.Web;  clientContext.Load(web, a => a.ServerRelativeUrl);  //clientContext.ExecuteQuery();  Microsoft.SharePoint.Client.List documentsList = clientContext.Web.Lists.GetByTitle("01.Diários");   var fileCreationInformation = new FileCreationInformation();  //Assign to content byte[] i.e. documentStream   fileCreationInformation.Content = System.IO.File.ReadAllBytes(@"C:\test\1. Verificar ficheiros ATM, VivaGO.xlsx");  //Allow owerwrite of document   fileCreationInformation.Overwrite = true;  //Upload URL   fileCreationInformation.Url = "https://****.sharepoint.com/sites/*****/Documentos/General/Reports/02.DFP_e_DMC/" + "1. Verificar ficheiros ATM, VivaGO.xlsx";   Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);   //Update the metadata for a field having name "DocType"  uploadFile.ListItemAllFields["Title"] = "UploadedviaCSOM";   uploadFile.ListItemAllFields.Update();  clientContext.ExecuteQuery();  }*/   

    But i´m getting forbideen Error. I´m conected throw VPN and all the user/pass are corret. Can you please help to understand the reason thats not working please?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.