如何使用c#在Sharepoint库子文件夹中上传文件?

时间:2022-10-22 20:27:53

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:

我需要在sharepoint库中使用c#控制台应用上传一个文件。我设法把它上传到父库。但是要求是把它上传到它的子文件夹。这是文件夹结构:

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

根文件夹—子文件夹1—子文件夹2—子文件夹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.

我需要把它上传到子文件夹3。现在,我只能上传到根文件夹。当我尝试在GetByTitle方法中输入子文件夹3时,它会抛出一个错误,但是当它是根文件夹时,它将成功上传。

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?
        }
    }
}

2 个解决方案

#1


8  

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

在使用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/

    库名(url)是文档,具有以下文件夹结构:文件夹/子文件夹/子文件夹/子文件夹/子文件夹

  2. Folder structure already exists

    文件夹结构已经存在

Using FileCreationInformation.Url property

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

使用FileCreationInformation。Url属性指定上传文件的文件夹Url。

The following example demonstrates how to specify relative url (a slightly modified version of your example, the main difference comes in specifying FileCreationInformation.Url)

下面的示例演示了如何指定相对url(示例的稍微修改版本,主要区别在于指定filecreationinfo . 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:

使用网络。GetFolderByServerRelativeUrl方法,用于检索必须上传文件的文件夹:

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

#2


0  

You need to get sub folder within folder. like below

您需要在文件夹中获取子文件夹。像下面的

docs.RootFolder.SubFolders

Remaining things is same, just need to add in same folder.

其余的都是一样的,只需要添加到相同的文件夹中。

try to go into folder like below

尝试进入如下所示的文件夹

docs.Folders["FolderName"]

#1


8  

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

在使用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/

    库名(url)是文档,具有以下文件夹结构:文件夹/子文件夹/子文件夹/子文件夹/子文件夹

  2. Folder structure already exists

    文件夹结构已经存在

Using FileCreationInformation.Url property

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

使用FileCreationInformation。Url属性指定上传文件的文件夹Url。

The following example demonstrates how to specify relative url (a slightly modified version of your example, the main difference comes in specifying FileCreationInformation.Url)

下面的示例演示了如何指定相对url(示例的稍微修改版本,主要区别在于指定filecreationinfo . 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:

使用网络。GetFolderByServerRelativeUrl方法,用于检索必须上传文件的文件夹:

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

#2


0  

You need to get sub folder within folder. like below

您需要在文件夹中获取子文件夹。像下面的

docs.RootFolder.SubFolders

Remaining things is same, just need to add in same folder.

其余的都是一样的,只需要添加到相同的文件夹中。

try to go into folder like below

尝试进入如下所示的文件夹

docs.Folders["FolderName"]