Oct 21, 2011

Get All Items including inside SharePoint Folder Programmatically using SPQuery

Just sharing the knowledge, I want to share about how to get all items / documents programmatically without getting inside the folder and querying it again. So, without folders, all items whether inside a folder or not, will be shown upon your query result.

Awwww, I’m not in Windows 2008 mode to copy my code in here and show some screenshots, but that’s just fine Smile with tongue out.

Using RecursiveAll in SPQuery
  1. SPQuery Q = new SPQuery();
  2. Q.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>BOOK</Value></Eq></Where>";
  3. Q.ViewFields = "<FieldRef Name='Name' /><FieldRef Name='ID' />";
  4. Q.ViewAttributes = "Scope='RecursiveAll'";

After you code your query, then you can add ViewAttributes and add Scope=’RecursiveAll’ to the SPQuery object.

Done, and all items whether inside a folder or not, will be shown upon your query result.