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)


Public Property Folder As SPFolder
Dim instance As SPQuery
Dim value As SPFolder

value = instance.Folder

instance.Folder = value
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.

Dim webSite As SPWeb = SPContext.Current.Site.RootWeb
    webSite.Lists.IncludeRootFolder = True
    Dim list As SPList = webSite.Lists("Document_Library_Name")
    Dim subFolder As SPFolder = _

    Dim query As New SPQuery()
    query.Folder = subFolder
    Dim items As SPListItemCollection = list.GetItems(query)

    Dim item As SPListItem
    For Each item In  items
        Response.Write((SPEncode.HtmlEncode(item.File.Name) + "<BR>"))
    Next item
End Try
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.

See also


SPQuery class

SPQuery members

Microsoft.SharePoint namespace