Maybe this was easy for everyone else but I have to admit that this wasn’t easy for me. As you may know I have developed a tool for SharePoint 2010 Site Provisioning in order to enhance the SharePoint development process which wasn’t able to programmatically create folders and subfolder in a document library or list.
So while I tried to change this I had a lot of trouble to achieve my goal.
Programmatically create a folder in a document library
This is the desired result:

I was able to successfully create a folder in a document library using this code:
SPList list = web.Lists.TryGetList("ListTitle");
SPFolderCollection folderColl = list.RootFolder.SubFolders;
SPFolder newFolder = folderColl.Add(FolderUrl);
FolderUrl means one of the following:
- "/Document Library/Folder1"
Creates a folder named “Folder1” under “Document Library”…
- "/Document Library/Folder1/Folder11"
Creates a folder named “Folder11” under “Folder1” in “Document Library”…
- "/Subsite/Document Library/Folder1"
Creates a folder named “Folder1” under “Document Library” in a sub site…
- "/Subsite/Document Library/Folder1/Folder11"
Creates a folder named “Folder11” under “Folder1” in “Document Library” in a sub site…
You need to use an URL including the name of the new folder which is different to creating a folder in a list.
Programmatically create a folder in a list
So after we can create a folder in a SharePoint document library we now can create them in SharePoint lists.
This is the desired result:

I was able to successfully create a folder in a SharePoint list using this code:
SPList list = web.Lists.TryGetList("ListTitle");
SPListItem newFolderItem = list.Items.Add(completeFolderUrl, SPFileSystemObjectType.Folder);
newFolderItem["Title"] = "TitleOfTheFolder";
newFolderItem.Update();
completeFolderUrl means one of the following:
- "/lists/Custom List"
The folder is created under “Custom List”…
- "/lists/Custom List/Folder1"
The folder is created under “Folder1” in “Custom List”…
- "/aboutus/lists/Custom List"
The folder is created under “Custom List” in a sub site…
- "/aboutus/lists/Custom List/Folder1"
The folder is created under “Folder1” in “Custom List” in a sub site…
Important: The folder didn’t create for me when my “completeFolderUrl” ended with a slash ‘/’.
Summary
I don’t know if I’m the only one who had problems to programmatically create folders and subfolder in a document library or list but I hope this helps someone else before you bang your head against the wall or table. 
Hope you like it...