Winsmarts.com

Microsoft MVP

MVP Logo

Awarded the Microsoft MVP Award.

Hosted By

blah!bLaH!BLOG!!

How to programatically find list items within a folder

Posted on 5/21/2007 @ 9:38 PM in #Sharepoint | 1 comments | 4977 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) ;

 

    Console.WriteLine(items.Count);

}

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


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

Please post your comments:


Your feedback will be submitted for moderation, and will appear after it is approved.

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Site designed and maintained by Sahil Malik | All Rights Reserved. ©2007 WinSmarts.com.