使用 C# 列出 Sharepoint 在线文件夹中的所有文件

List files all files in folder on Sharepoint online with C#

正在尝试列出 Sharepoint online 目录中的所有文件。但是在 Microsoft.SharePoint.Client.ServerException 行出现异常:'列表 'Shared%20Documents' 在 URL 'https://MySite.sharepoint.com/sites/TeamSite' 的站点不存在。'

目录存在,检索目录时在目录下找到。不知道从 Sharepoint 在线检索文件的方法是否正确。

using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System.Collections.Generic;
using System.IO;

namespace Sharepoint
{
class Program
{
    static void Main(string[] args)
    {
        string siteUrl = "https://MySite.sharepoint.com/sites/TeamSite";
        List<ListItem> items = new List<ListItem>();

        using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, appId, AppSecred))
        {
            Web web = clientContext.Web;

            clientContext.Load(web);
            clientContext.Load(web.Lists);
            clientContext.Load(web, wb => wb.ServerRelativeUrl);
            clientContext.ExecuteQuery();

            Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/Shared%20Documents/Testfolder/");
            clientContext.Load(folder);
            clientContext.ExecuteQuery();

            List list = web.Lists.GetByTitle("Shared%20Documents");
            clientContext.Load(list);
            clientContext.ExecuteQuery(); //Throws an exception. Microsoft.SharePoint.Client.ServerException: 'List 'Shared%20Documents' does not exist at site with URL 'https://MySite.sharepoint.com/sites/TeamSite'.'

            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = @"<View Scope='Recursive'><Query></Query></View>";
            camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
            ListItemCollection listItems = list.GetItems(camlQuery);
           
            // Todo
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();
        };
    }
}

}

尝试使用 Documents 而不是 Shared Documents,或者您始终可以使用 context.web.Lists.GetbyID(system.GUID('yourlistguid')) 通过其 id

获取列表