Create Sub Folders in Lists Programmatically

I had a requirement to use the new sub folders feature of WSS 3.0 for lists and had a dig around the object model on a way to implement it.

The SPList object has a handy way of referencing the subfolders through _spList.RootFolder.SubFolders and there are methods on the returned object that allow you to manipulate the folders.

Such a method is the Add method which takes a folderName parameter – great!

However, if you create a folder this way and look at the list through the browser, you will not find the folder.  It is there, but it is completely hidden!

Perversely, Redmond decided that this was not the way to create folders in WSS 3.0, and they are actually created using the following code:

SPListItem newFolder = _spList.Items.Add( "", SPFileSystemObjectType.Folder, folderName);

newFolder.Update();

Basically you are creating a new list item with a 'Folder' type.

Thanks to Paul, our Microsoft consultant, for locating the correct way of doing this!

Leave a Reply