Export (0) Print
Expand All

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 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.

NoteNote

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) + 
          "<BR>");
    }
}
NoteNote

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.

Show:
© 2014 Microsoft