How to upload a file to a subfolder of a Sharepoint library using C #?

I need to upload a file using the C # console application in the sharepoint library. I manage to load it only in the parent library. But you need to upload it to your subfolder.
So, the folder structure:

The root folder
-Sub Folder 1
--- Subfolder 2
---- Subfolder 3

I need to upload it to subfolder 3. Now I can only upload the root folder. It throws an error when I tried to enter subfolder 3 in the GetByTitle method, but when it is the root folder, it will be loaded.

Here is 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 next? } } } 
+6
source share
2 answers

There are several options for specifying a subfolder when downloading a file using CSOM

There are two assumptions about the solutions provided below:

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

  • Folder structure already exists

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); } 
+9
source

You need to get the folder in the folder. as below

 docs.RootFolder.SubFolders 

The rest of the things are the same, just need to be added to the same folder.

try to enter the folder as shown below

 docs.Folders["FolderName"] 
0
source

Source: https://habr.com/ru/post/1201997/


All Articles