Export (0) Print
Expand All

SPDocumentLibrary.GetItemsInFolder Method

Windows SharePoint Services 3

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)

public SPListItemCollection GetItemsInFolder(
	SPView view,
	SPFolder folder
)

Parameters

view
Type: Microsoft.SharePoint.SPView
An SPView object representing the view through which to retrieve items from the document library.
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.

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.

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.

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>";
    }
}
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 Best Practices: Using Disposable Windows SharePoint Services Objects.

Community Additions

ADD
Show:
© 2014 Microsoft