How to programatically find list items within a folder

Posted on 5/21/2007 @ 9:38 PM in #SharePoint by | Feedback | 6179 views

Okay, this is not straightforward - atleast not so because we tend to think in terms of directories and files.

Now you know that lists can have folders, and folders can have items underneath. Here is a peice of code that will let you query for items in a given folder inside a list, as long as you know the URL to that folder.

using (SPSite site = new SPSite(mySPSite))


    SPWeb web = site.OpenWeb();

    SPList list = web.Lists[myList];

    SPFolder folderInstance = list.RootFolder.SubFolders[folderUrl];


    SPQuery query = new SPQuery() ;

    query.Folder = folderInstance;


    SPListItemCollection items = list.GetItems(query) ;




At the end of the using block, the items variable will have the list items in the specified folder at folderUrl, on site mySPSite, on list myList (these are all string variables).

Sound off but keep it civil:

Older comments..

On 9/3/2008 11:59:56 AM tidwtf said ..
Just what I was looking for. Why don't they make it easy?! Thanks