遍历SharePoint列表所有文件和文件夹并在TreeView中展示

遍历SharePoint列表所有文件和文件夹并在TreeView中展示

1.说明

使用SharePoint.Net客户端模型,将SharePoint列表可视化,遍历列表文件和文件夹并展示在TreeView上。

2.操作

下面通过编写一个winform程序来完成效果展示。

1)添加控件

创建一个主窗体,添加TreeView控件,Button和DomainUpDown控件

2)窗体载入

档窗体载入时根据客户端模型自动获取SharePoint的列表文档库,放入DomainUpDown中

  private void SharePointFrm_Load(object sender, EventArgs e)
        {
            using (ClientContext clientcontext = new ClientContext(ServerURL))
            {
                clientcontext.Credentials = new System.Net.NetworkCredential("域账号", "域账号密码", "域名");
                //读取列表
                clientcontext.Load(clientcontext.Web.Lists);
                clientcontext.ExecuteQuery();
                foreach (List list in clientcontext.Web.Lists)
                {
                    //DomainUpDown加入库表名
                    ddlLists.Items.Add(list.Title);
                }
            }
        }

3)展示列表

点击按钮,根据列表名,打开列表,遍历文档库的所有文件夹和文件,依次在TreeView中加入相应的Nodes

private void btnOpen_Click(object sender, EventArgs e)
        {
            using (ClientContext clientcontext = new ClientContext(ServerURL))
            {
                clientcontext.Credentials = new System.Net.NetworkCredential("域账号", "域账号密码", "域名");
                try
                {
                    List list = clientcontext.Web.Lists.GetByTitle(ddlLists.SelectedItem.ToString());
                    clientcontext.Load(list);
                    clientcontext.ExecuteQuery();
                    clientcontext.Load(list.RootFolder);
                    clientcontext.Load(list.RootFolder.Folders);
                    clientcontext.ExecuteQuery();
                    TreeViewLibraries.ShowLines = true;
                    TreeNode RootNode = new TreeNode(list.Title);
                    //RootNode1.Add(RootNode);
                    TreeViewLibraries.Nodes.Add(RootNode);

                    IterateListItems(RootNode, list.RootFolder, clientcontext);

                    foreach (Folder SubFolder in list.RootFolder.Folders)
                    {
                        if (SubFolder.Name != "Forms")
                        {
                            TreeNode TopNode = new TreeNode(SubFolder.Name);
                            RootNode.Nodes.Add(TopNode);
                            IterateListItems(TopNode, SubFolder, clientcontext);
                            IterateFolders(TopNode, SubFolder, clientcontext);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + "\r\n出错位置");
                }
            }
            //展开TreeView
            TreeViewLibraries.ExpandAll();
        }

遍历文件方法

public void IterateListItems(TreeNode MainNode, Folder folder, ClientContext clientcontext)
        {
            clientcontext.Load(folder.Files);
            clientcontext.ExecuteQuery();
            foreach (File file in folder.Files)
            {
                TreeNode SubNode = new TreeNode(file.Name);
                MainNode.Nodes.Add(SubNode);
            }
        }

遍历文件夹方法

public void IterateFolders(TreeNode MainNode, Folder subFolder, ClientContext clientcontext)
        {
            clientcontext.Load(subFolder.Folders);
            clientcontext.ExecuteQuery();
            foreach (Folder folder in subFolder.Folders)
            {
                TreeNode SubNode = new TreeNode(folder.Name);
                MainNode.Nodes.Add(SubNode);
                IterateListItems(SubNode, folder, clientcontext);
                IterateFolders(SubNode, folder, clientcontext);
            }
        }

参考

以上为个人整理总结的知识,如有遗漏或错误欢迎留言指出、点评,如要引用,请写联系通知,未经允许谢绝转载。
[1]: https://www.codeproject.com/Tips/634824/View-SharePoint-List-Items-in-a-TreeView

你可能感兴趣的:(SharePoint)