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

24,738

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);   
}
Share:
24,738
kempyyyy
Author by

kempyyyy

Updated on August 03, 2022

Comments

  • kempyyyy
    kempyyyy almost 2 years

    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?
            }
        }
    }
    
  • kempyyyy
    kempyyyy over 9 years
    there is no method Folders in the docs object. The compilers shows a syntax error.
  • Ali Murtaza
    Ali Murtaza over 9 years
    Yes. But with Root Folder you have option. Check with this link [link]sharepoint.stackexchange.com/questions/41131/…
  • dotnetN00b
    dotnetN00b about 9 years
    Is "Root Folder" referring to "Documents" or "Folders"?
  • Vadim Gremyachev
    Vadim Gremyachev about 9 years
    List.RootFolder refers to "Documents"
  • dotnetN00b
    dotnetN00b about 9 years
    var list = context.Web.Lists.GetByTitle("Root Folder"); I was referring to this line in case I wasn't clear.
  • Vadim Gremyachev
    Vadim Gremyachev about 9 years
    Since this method accepts the list title, "Root Folder" refers to List/Library object
  • DARKGuy
    DARKGuy over 7 years
    What if the folder structure does not exist? how to create it? This answer needs a bit more information :(
  • Ravi Khambhati
    Ravi Khambhati over 5 years
    I used FileCreationInformation approach and file shows me checked out. Do we have to check the file as well?
  • Ariwibawa
    Ariwibawa about 3 years
    I found it easier to use GetFolderByServerRelativeUrl, because when I used GetByTitle. It threw File Not Found.