【问题标题】:List all files in a Sharepoint online Document Library列出 Sharepoint 在线文档库中的所有文件
【发布时间】:2019-02-09 21:05:10
【问题描述】:

我正在尝试列出 Sharepoint 文档列表中的所有文件,

但是我无法找到访问文档库内容列表的方法,

我能够打印 Sharepoint 包含的所有列表的名称,但不能打印文档库中的文件,

这是我的示例代码:

private static void FList(ICredentials credentials)
{
    ClientContext ctx = new ClientContext(" SHAREPOINT ADDRESS");
    ctx.Credentials = credentials; 

    List doclib = ctx.Web.Lists.GetByTitle("Reporting Rosters"); 
    ctx.Load(ctx.Web.Lists);
    ctx.ExecuteQuery();

    foreach (var list in ctx.Web.Lists)
    {
        Console.WriteLine(list.Title);
    }

结果是 Sharepoint 列表的列表,如果有人可以指导我如何在 sharepoint 文档库中公开文件名,我将不胜感激

【问题讨论】:

    标签: c# sharepoint sharepoint-online


    【解决方案1】:

    这里是实际遍历 Files 、子文件夹和这些子文件夹中的文件的代码

    var credentials = new SharePointOnlineCredentials(username, securedPassword);
    private static void Flist3(ICredentials credentials)
        {
    
            ClientContext clientContext = new ClientContext("SHAREPOINT ADDRESS");
            clientContext.Credentials = credentials;  // passing credentials in case you need to work with Sharepoint Online
            using (clientContext)
            {
                List list = clientContext.Web.Lists.GetByTitle("Document Library Name");
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<View Scope='Recursive'><Query></Query></View>";
                Folder ff = list.RootFolder;
                FolderCollection fcol = list.RootFolder.Folders; // here you will save the folder info inside a Folder Collection list
                List<string> lstFile = new List<string>();
                FileCollection ficol = list.RootFolder.Files;   // here you will save the File names inside a file Collection list 
                // ------informational -------
                clientContext.Load(ff);
                clientContext.Load(list);
                clientContext.Load(list.RootFolder);
                clientContext.Load(list.RootFolder.Folders);
                clientContext.Load(list.RootFolder.Files);
                clientContext.ExecuteQuery();
                Console.WriteLine("Root : " + ff.Name + "\r\n");
                Console.WriteLine(" ItemCount : " + ff.ItemCount.ToString());
                Console.WriteLine(" Folder Count : " + ff.Folders.Count.ToString());
                Console.WriteLine(" File Count : " + ff.Files.Count.ToString());
    
                Console.WriteLine(" URL : " + ff.ServerRelativeUrl);
                //---------------------------
                //---------Here you iterate through the files and not the folders that are in the root folder ------------
                foreach (ClientOM.File f in ficol)
                {
                    Console.WriteLine("Files Name:" + f.Name);
                }
                //-------- here you will iterate through the folders and the files inside the folders that reside in the root folder----
                foreach (Folder f in fcol)
                {
                    Console.WriteLine("Folder Name : " + f.Name);
                    clientContext.Load(f.Files);
                    clientContext.ExecuteQuery();
                    FileCollection fileCol = f.Files;
                    foreach (ClientOM.File file in fileCol)
                    {
                        lstFile.Add(file.Name);
                        Console.WriteLine(" File Name : " + file.Name);
    
                    }
    

    }

    【讨论】:

    • 这是否处理多级文件夹层次结构?
    【解决方案2】:

    您需要遍历 ListItemCollection 而不是 ListCollection。

    为此,您需要使用 CAML 查询获取列表中的所有项目,然后对其进行迭代。

    所以,修改你的代码如下:

    List doclib = ctx.Web.Lists.GetByTitle("Reporting Rosters"); 
    ctx.Load(doclib);
    ctx.ExecuteQuery();
    
    Microsoft.SharePoint.Client.CamlQuery camlQuery = CamlQuery.CreateAllItemsQuery();
    ListItemCollection listItems = doclib.GetItems(camlQuery);
    clientContext.Load(listItems);
    clientContext.ExecuteQuery();
    
    foreach (var listItem in listItems)
    {
        Console.WriteLine(listItem["FileLeafRef"].ToString());  // gives the file name
        Console.WriteLine(listItem["FileRef"].ToString());  // gives the file's server relative URL
    }
    

    【讨论】:

      【解决方案3】:

      你可以试试这个:

          ClientContext cxt = new ClientContext("SHAREPOINT ADDRESS");
          List doclib = cxt.Web.Lists.GetByTitle("Reporting Rosters");
      
          cxt.Load(doclib);
          cxt.Load(doclib.RootFolder);
          cxt.Load(doclib.RootFolder.Folders);
          cxt.Load(doclib.RootFolder.Files);
          cxt.ExecuteQuery();
          FolderCollection fol = doclib.RootFolder.Folders;
          List<string> listFile = new List<string>();
          foreach(Folder f in fol)
          {
              if (f.Name == "filename")
              {
                  cxt.Load(f.Files);
                  cxt.ExecuteQuery();
                  FileCollection fileCol = f.Files;
                  foreach (File file in fileCol)
                  {
                      listFile.Add(file.Name);
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2021-09-22
        • 2023-01-13
        • 2019-10-20
        • 2021-03-26
        • 2013-05-01
        • 2014-02-12
        • 1970-01-01
        • 2010-12-06
        • 1970-01-01
        相关资源
        最近更新 更多