Share via


retrieve a folder using CAML query

Question

Monday, December 5, 2011 7:14 AM

Hi,

I have a Document library which contains multiple folders.

Libray-

folder1

folder 2    and so on

Since the no: of folders is quite huge I do not want to iterate through each. I know the foldername though.

How do I retrieve the folder when Folder name is known using CAML query.

All replies (3)

Monday, December 5, 2011 8:29 AM ✅Answered

Hi,

Have you checkd this

http://stackoverflow.com/questions/1557675/caml-query-that-includes-folders-in-result-set

http://stackoverflow.com/questions/2922826/how-to-get-a-folder-from-caml-query

Hope this links will help you

 

Thanks.... ________________ Baba (MCTS, MCPD)


Monday, December 5, 2011 8:49 AM ✅Answered

Here is the code snippet for reading the items from a particular folder

            SPSite site = new SPSite("http://<url of your site>");
            SPList list = site.RootWeb.Lists["CUSTOM_LIST"];
            SPFolder folder = site.RootWeb.GetListItem("/Lists/CUSTOM_LIST/folder1").Folder;
            //please create the above objects as per your design
       
            SPQuery query = new SPQuery();
            //Set the Folder property of SPQuery to the required folder object created above
            query.Folder = folder;
            //Assign the required CAML query to the Query property of SPQuery (if needed)

            SPListItemCollection items = list.GetItems(query);
            //now items collection will have the items from folder1 of CUSTOM_LIST

The same can used for Documents Libraries as well (with proper URL of the folder)
Hope this is helpful

- Ram.


Monday, December 5, 2011 8:46 AM

If you want to query all folders and sub folders of a list or document library, you have to define extra query options. If you are working with the object model you have to set the ViewAttributes property of the SPQuery object as follows:

qry.ViewAttributes = "Scope='Recursive'";

If you work with GetListItems method of the Lists.asmx SharePoint web service, you have to define an extra node with the QueryOptions element:

XmlNode queryOptionsNode = camlDocument.CreateElement("QueryOptions");

queryOptionsNode.InnerXml = "<ViewAttributes Scope=\"Recursive\" />";

Senior Software Developer - Virtosoftware - Premium Web Parts for Microsoft SharePoint 2007 & 2010