Everything SharePoint and XAML

My continuous learning of SharePoint, XAML, Silverlight, Windows Phone 7, Windows 8, Office, VSTO, C#…

Iterate through Items in a List

Posted by Steve Pietrek on September 21, 2007

As a way to learn the SharePoint Object Model, I decided to create an application which will display all items (e.g. Sites, Subsites, Content Types, Features, Fields, Lists, Users, etc.) for a top-level site. I know there are other ones out there (SharePoint Inspector, Feature Explorer, etc.) but they don’t have all the features I want (I have ideas!) and what fun is it using someone else’s when you can create your own.

Though I have a good first step, I’m thinking about going back to the drawing board to use web services instead. Although the current application works great, it will only work when run on the SharePoint server. It may not be as fast, but web services will allow the application to be run remotely. Plus I get to play with something new!

Enough talk, one feature I wanted was to display the items in a list and files in a document library. Fine you ask. Iterating through files in a document library is easy-cheesy (just use subfolders and recursively call the method).

private void LoadFolderItems(SPFolder folder, BaseNode node) {

BaseNode node2;


foreach (SPFolder childFolder in folder.SubFolders) {

if (childFolder.Name != “Forms”) {

node2 = new FolderNode(childFolder, node);



LoadFolderItems(childFolder, node2);




foreach (SPFile file in folder.Files) {

node2 = new FileNode(file, node);




Great, but what about Items in a list. Uh oh. Subfolders no longer work. Not to fear, use GetItems.


private static void LoadFolderItems(SPFolder folder, BaseNode node) {

BaseNode node2;

SPQuery qry = new SPQuery();

qry.Folder = folder;


SPWeb web = null;


try {

web = folder.ParentWeb;


SPListItemCollection ic = web.Lists[folder.ParentListId].GetItems(qry);


foreach (SPListItem item in ic) {

node2 = new ItemNode(item, node);



if (item.Folder != null)

LoadFolderItems(item.Folder, node2);



finally {

if (web != null)





Sorry, the comment form is closed at this time.

%d bloggers like this: