SPDocumentLibrary.GetItemsInFolder method

Returns a collection of items from the document library based on the specified view and folder.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Function GetItemsInFolder ( _
    view As SPView, _
    folder As SPFolder _
) As SPListItemCollection
'Usage
Dim instance As SPDocumentLibrary
Dim view As SPView
Dim folder As SPFolder
Dim returnValue As SPListItemCollection

returnValue = instance.GetItemsInFolder(view, _
    folder)
public SPListItemCollection GetItemsInFolder(
    SPView view,
    SPFolder folder
)

Parameters

  • folder
    Type: Microsoft.SharePoint.SPFolder

    An SPFolder object representing the folder from which to retrieve items. If a null reference (Nothing in Visual Basic), returns items in the root folder of the list.

Return value

Type: Microsoft.SharePoint.SPListItemCollection
An SPListItemCollection object that represents the documents.

Remarks

If the document library contains one or more subfolders and the library itself is passed as the folder parameter, the GetItemsInFolder method returns all items in the top-level folder, including the subfolders, but excluding any items within the subfolders.

Examples

The following code example uses the GetItemsInFolder method to return the IDs of all items within the subfolder. The integer IDs are indexed with respect to the overall collection of items contained within the library.

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim site As SPWeb = siteCollection.AllWebs("Site_Name")

Dim docLibName As String = "DocLibrary_Name"
Dim docLibFolder As SPFolder = site.Folders(docLibName)
Dim docLibInnerFolder As SPFolder = 
    docLibFolder.SubFolders("Subfolder_Name")

Dim docLib As SPDocumentLibrary = CType(site.Lists(docLibName), 
    SPDocumentLibrary)
Dim docLibView As SPView = docLib.Views("View_Name")

Dim docLibItems As SPListItemCollection = 
    docLib.GetItemsInFolder(docLibView, docLibInnerFolder)
Dim i As Integer

For i = 0 To docLibItems.Count - 1

    Label1.Text += docLibItems(i).ID.ToString() & "<BR>"

Next i 
SPSite oSiteCollection = SPContext.Current.Site;
using(SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name"])
{
    string strDocLibName = "DocLibrary_Name";
    SPFolder oFolderParent = oWebsite.Folders[strDocLibName];
    SPFolder oFolderChild = oFolderParent.SubFolders["Subfolder_Name"];

    SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)oWebsite.Lists[strDocLibName];
    SPView oView = oDocumentLibrary.Views["View_Name"];
    SPListItemCollection collListItems = oDocumentLibrary.GetItemsInFolder(oView, oFolderChild);

    for (int intIndex = 0; intIndex < collListItems.Count; intIndex++)
    {
        Label1.Text += collListItems[intIndex].ID.ToString() + "<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

SPDocumentLibrary class

SPDocumentLibrary members

Microsoft.SharePoint namespace