Wednesday, February 25, 2009

Getting the listItems reside inside a SharePoint folder

One of our project's form library was designed with folders due to the 2000 items per view limitation. (Forms submitted in January will be kept inside a folder called "Jan08")
As part of some other requirement we had to access only the items inside a particular folder. Here is the code we found after some time of investigation with the SPQuery object.

try
{
//25/02/2009- FEB Get only the items specific to a folder
SPSite site = new SPSite(
http://mysite:6666/sites/lat);
SPWeb web = site.OpenWeb();
SPList list = web.Lists["DocumentLibrary1"];
SPQuery query = new SPQuery();
SPFolder myFolder = list.RootFolder.SubFolders["MyFolder_001"];
query.Folder = myFolder;
SPListItemCollection folderItems = list.GetItems(query);
foreach (SPListItem item in folderItems)
{
//Do something here with the items belongs to the folder "MyFolder_001"
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}


Hope if it helps for some people.

No comments: