Search code examples
c#.netsharepointfile-uploadsharepoint-2010

How to upload a file in a Sharepoint library subfolder using c#?


I need to upload a file using c# console app in a sharepoint library. I manage to upload it to the parent library only. But the requirement is to upload it on its sub folder.
So here's the Folder Structure:

Root Folder
-Sub Folder 1
---Sub Folder 2
----Sub Folder 3

I need to upload it in Sub Folder 3. Right now, I'm only able to upload on the Root Folder. It throws an error when I tried to input the Sub Folder 3 in the GetByTitle method, but when it's the root folder, it is succeeding to upload.

Here's my code.

using (ClientContext clientContext = new ClientContext(siteURL))
{
    clientContext.Credentials = new System.Net.NetworkCredential(@"username", "password", "domain");

    var web = clientContext.Web;

    // Create the new file  
    var newFile = new FileCreationInformation();
    newFile.Content = System.IO.File.ReadAllBytes(@"C:\filepath\test.xlsx");
    newFile.Overwrite = true;
    newFile.Url = "Test Upload.xlsx";

    List list = web.Lists.GetByTitle("Service Oriented Architecture (SOA)");
    clientContext.Load(list);
    clientContext.ExecuteQuery();
    clientContext.Load(list.RootFolder);
    clientContext.Load(list.RootFolder.Folders);

    clientContext.ExecuteQuery();

    foreach (Folder SubFolder in list.RootFolder.Folders)
    {
        if (SubFolder.Name.Equals("07 - SOA Environment"))
        {
            //What's next?
        }
    }
}

Solution

  • There are several options how to specify sub folder while uploading the file using CSOM

    There are two assumptions about the provided solutions below:

    1. The library name(url) is Documents and has the following folder structure: Folder/Sub Folder/Sub Sub Folder/Sub Sub Sub Folder/

    2. Folder structure already exists

    Using FileCreationInformation.Url property

    Use FileCreationInformation.Url property to specify the folder url for a uploaded file.

    The following example demonstrates how to specify relative url (a slightly modified version of your example, the main difference comes in specifying 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 Web.GetFolderByServerRelativeUrl method

    Use Web.GetFolderByServerRelativeUrl method to retrieve a folder where file have to be uploaded:

    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();
    }
    

    Usage

    using (var ctx = new ClientContext(webUri))
    {
         ctx.Credentials = credentials;
    
         UploadFile(ctx,"Documents/Folder/Sub Folder/Sub Sub Folder/Sub Sub Sub Folder",filePath);   
    }