Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have SharePoint Library and i want to traverse all folder and sub folder of it using client object model. Can anybody have solution ?

share|improve this question
add comment

3 Answers

up vote 9 down vote accepted

The Following code will Display all Libraries and Folders of each Library in Treeview

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

You can modify the code as per your requirement :-)

Output :

enter image description here

share|improve this answer
add comment

Funnily enough ... I'd this today ... here you go ...

NetworkCredential credentials = new NetworkCredential("username", "password", "domain");
        ClientContext clientContext = new ClientContext("http://sharepoint/web");
        clientContext.Credentials = credentials;
        CamlQuery query = new CamlQuery();
        query.ViewXml = "<View Scope='Recursive' />";
        ListItemCollection docs = clientContext.Web.Lists.GetByTitle("ListName").GetItems(query);
        clientContext.Load(docs);
        clientContext.ExecuteQuery();

After that just foreach through the listitems returned by docs.

share|improve this answer
2  
It should be query.ViewXml = "<View Scope='RecursiveAll ' />"; Even the sub folders must be traversed. –  Vardhaman Deshpande Jul 17 '12 at 21:04
    
Or simply, you can do the following: CamlQuery query = CreateAllFoldersQuery(); –  Vardhaman Deshpande Jul 17 '12 at 21:06
add comment

You can do it through SPQuery:

SPQuery query = new SPQuery();

//Condition to check the item type is folder or not
query.Query = “<Where><Eq><FieldRef Name=’FSObjType’/><Value Type=’Lookup’>1</Value></Eq></Where>”;

//Get all the items including subfolders from the list 
query.ViewAttributes = “Scope=’RecursiveAll’”;

look at this post

share|improve this answer
1  
SPQuery is not a part of Client Object Modal. You can not use this in COM. –  Vikalp Kapadiya Jul 17 '12 at 12:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.