SPQuery.Folder Property

Gets or sets the folder within a document library from which to return items in the query.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

public SPFolder Folder { get; set; }

Property Value

Type: Microsoft.SharePoint.SPFolder
An SPFolder object that represents the folder.

The following code example uses the Folder property to specify the subfolder from which to return items in a document library.


For information about how to use Language-Integrated Query (LINQ) queries to retrieve list items in SharePoint Foundation, see Managing Data with LINQ to SharePoint.

This example requires using directives (Imports in Microsoft Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)

    oWebsiteRoot.Lists.IncludeRootFolder = true;
    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);

    foreach (SPListItem oListItem in collListItems)
        Response.Write(SPEncode.HtmlEncode(oListItem.File.Name) + 

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.