Saturday 3 September 2011

Using Folder,FilesOnly,Recursive,RecursiveAll in SPquery in SharePoint


Using the Folder attribute with SPquery - This is used when you want to Retrive items from a specific folder only. The msdn example says "Gets or sets the folder within a document library from which to return items in the query."

example -

SPList oList = oWebsiteRoot.Lists["Document_Library_Name"];
SPFolder oFolder = oList.RootFolder.SubFolders["Folder_Name"];

SPQuery oQuery = new SPQuery();
oQuery.Folder = oFolder;
SPListItemCollection collListItems = oList.GetItems(oQuery);

Using FilesOnly attribute with SPquery - FilesOnly will get all the files of a specific folder.

SPList oList = oWebsiteRoot.Lists["Document_Library_Name"];
SPFolder oFolder = oList.RootFolder.SubFolders["Folder_Name"];

SPQuery oQuery = new SPQuery();
oQuery.Folder = oFolder;
query.ViewAttributes = "Scope=\"FilesOnly\"";
SPListItemCollection collListItems = oList.GetItems(oQuery);


Using Recursive attribute - Show all files and all subfolders of all folders.
SPList oList = oWebsiteRoot.Lists["Document_Library_Name"];
SPFolder oFolder = oList.RootFolder.SubFolders["Folder_Name"];

SPView view = list.Views["View_Name"];

SPQuery oQuery = new SPQuery(view);
oQuery.ViewAttributes = "Scope=\"Recursive\"";
SPListItemCollection myItems = list.GetItems(oQuery);

Using RecursiveAll attribute - Show all files and all subfolders of all folders.

SPList oList = oWebsiteRoot.Lists["Document_Library_Name"];
SPFolder oFolder = oList.RootFolder.SubFolders["Folder_Name"];

SPView view = list.Views["View_Name"];

SPQuery oQuery = new SPQuery(view);
oQuery.ViewAttributes = "Scope=\"RecursiveAll\"";
SPListItemCollection myItems = list.GetItems(oQuery);

Ads by Google

3 comments:

  1. So are you saying there is no difference between "Recursive" and "RecursiveAll"?

    ReplyDelete
  2. using "RecursiveAll" will all include the folders in the result.

    "Recursive" only returns the files in folders and subfolders.

    ReplyDelete
  3. This is very confusing. RecursiveAll for the root folder behaves different as if the query is for a subfolder. Add to injury that every "Caml Query Tool" available is extremely buggy.

    ReplyDelete