使用客户端对象模型Sharepoint 2010填充Treeview

时间:2012-07-06 21:17:08

标签: sharepoint-2010 wcf-client sharepoint-designer client-object-model sharepoint-clientobject

我有一个文档库。在文档库里面我有一个名为Studies的文件夹。根据研究,我还有10个文件夹和后续子文件夹。

我需要使用客户端对象模型SharePoint 2010在树视图中填充相同内容。

DocLibrary1>>Studies>>Study1- Folder1
                             -Folder2
                             -Folder3

我希望在一个函数中在树视图中发布它,我可以传递文档库并返回树视图。

1 个答案:

答案 0 :(得分:0)

以下代码将显示每个库的所有库和文件夹

private void frmForm1_Load(object sender, EventArgs e)
{
    using (ClientContext clientcontext= new ClientContext("http://your server"))
    {

        //Load Libraries from SharePoint
        clientcontext.Load(clientcontext.Web.Lists);
        clientcontext.ExecuteQuery();
        foreach (List list in clientcontext.Web.Lists)
        {
           try
           {
                if (list.BaseType.ToString() == "DocumentLibrary" && !list.IsApplicationList && !list.Hidden && list.Title != "Form Templates" && list.Title != "Customized Reports" && list.Title != "Site Collection Documents" && list.Title != "Site Collection Images" && list.Title != "Images")
                {
                    clientcontext.Load(list);
                    clientcontext.ExecuteQuery();
                    clientcontext.Load(list.RootFolder);
                    clientcontext.Load(list.RootFolder.Folders);
                    clientcontext.ExecuteQuery();
                    TreeViewLibraries.ShowLines = true;
                    TreeNode LibraryNode = new TreeNode(list.Title);
                    TreeViewLibraries.Nodes.Add(LibraryNode);
                        foreach (Folder SubFolder in list.RootFolder.Folders)
                        {
                            if (SubFolder.Name != "Forms")
                            {
                                TreeNode MainNode = new TreeNode(SubFolder.Name);
                                LibraryNode.Nodes.Add(MainNode);
                                FillTreeViewNodes(SubFolder, MainNode, clientcontext);
                            }
                        }

                }
            }

        }
    }
}


//Recursive Function

public void FillTreeViewNodes(Folder SubFolder, TreeNode MainNode, ClientContext clientcontext)
{
    clientcontext.Load(SubFolder.Folders);
    clientcontext.ExecuteQuery();
        foreach (Folder Fol in SubFolder.Folders)
        {
            TreeNode SubNode = new TreeNode(Fol.Name);
            MainNode.Nodes.Add(SubNode);
            FillTreeViewNodes(Fol, SubNode, clientcontext);
        }
}

您可以根据自己的要求修改代码: - )