Compartilhar via


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

Syntax

'Declaration
Public Property Folder As SPFolder
    Get
    Set
'Usage
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.

Examples

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

Note

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
Try
    webSite.Lists.IncludeRootFolder = True
    Dim list As SPList = webSite.Lists("Document_Library_Name")
    Dim subFolder As SPFolder = _
      list.RootFolder.SubFolders("Folder_Name")

    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
Finally
    webSite.Dispose()
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) + 
          "<BR>");
    }
}

Note

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

Reference

SPQuery Class

SPQuery Members

Microsoft.SharePoint Namespace