How To Create SharePoint Document Library Subfolders Programmatically
Don’t you just love it when you’re slaving away at something that should be pretty straight forward but you just can’t figure it out? Well I just had one of those breakthrough moments and thought I’d share. I spent almost the entire day yesterday searching and trying different ways to add sub folders to Document Libraries programmatically to no avail. I was seriously pulling my hairs out. And then just 30 mins ago, I came across this blog post and now I can go take a 3 hour lunch. Here’s my variation of the code:
private void AddFolders(SPList list, SPWeb web)
{
String url = list.RootFolder.ServerRelativeUrl.ToString();
SPFolderCollection folders = web.GetFolder(url).SubFolders;
folders.Add(”Yay this works”);
}
Long live the blog!


I’m really new at sharepoint and a I wanted to know what is this SPList that you use in the method.
ma
12 May 08 at 5:41 am
hey ma, please see here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.aspx
Henry
12 May 08 at 7:39 pm
Hi Henry….
I want to Create New Custom Document Library and add some SiteColumns or Content Types Programmatically….how can i do this…..plz help me out….
Saurabh
1 Jun 08 at 11:34 pm
How could this be done in VB6 or VBA?
Simon
4 Jul 08 at 12:11 am
Just curious - doesn’t this work?
list.RootFolder.SubFolders.Add(”Huh”);
Oskar Austegard
28 Jul 08 at 12:43 pm
Hey Oskar, it’s been a while since I’ve done any programming so I’m not sure. Have you tried it?
Henry
28 Jul 08 at 5:56 pm
SPListTemplateType templateType = SPListTemplateType.DocumentLibrary;
Guid listId = rootWeb.Lists.Add(”MyDocLibrary”, null, templateType);
SPlist documentLibrary= rootWeb.Lists[listId];
SPContentType objContentType= rootWeb.ContentTypes["MyContenttypeAdding"].Parent;
documentLibrary.ContentTypes.Add(new SPContentType(objContentType, rootWeb.ContentTypes, “MyContenttypeAdding”));
chanaka
23 Sep 08 at 12:37 am
Thanks Henry, this really helped!
Dathan
25 Sep 08 at 1:51 pm
I was trying to find a vb.net way to create a sub folder. So I figured it out here it is
Dim strfolder As String = “Test4″
Dim strlist As String = “Documents”
Dim strsite As String = “http://mossfe:8000/”
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
SPSecurity.RunWithElevatedPrivileges(AddressOf AddFolders)
End Sub
Private Sub AddFolders()
Dim site As SPSite = New SPSite(strsite)
Dim web As SPWeb = site.OpenWeb()
web.AllowUnsafeUpdates = True
Dim doclib As SPList = web.Lists(strlist)
Dim folders As SPFolderCollection = web.GetFolder(doclib.RootFolder.ServerRelativeUrl).SubFolders
Dim NewFolder As SPFolder = folders.Add(strfolder)
End Sub
Chears
David Steadman
14 Oct 08 at 4:40 pm