The SharePoint Engie that could…

Stories from a SharePoint Engineer that isn't afraid of Visual Studio.

How To Create SharePoint Document Library Subfolders Programmatically

with 17 comments

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!

Written by Henry

August 15, 2007 at 9:38 am

Posted in SharePoint

17 Responses

Subscribe to comments with RSS.

  1. I’m really new at sharepoint and a I wanted to know what is this SPList that you use in the method.

    ma

    May 12, 2008 at 5:41 am

  2. 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

    June 1, 2008 at 11:34 pm

  3. How could this be done in VB6 or VBA?

    Simon

    July 4, 2008 at 12:11 am

  4. Just curious – doesn’t this work?
    list.RootFolder.SubFolders.Add(”Huh”);

    Oskar Austegard

    July 28, 2008 at 12:43 pm

  5. Hey Oskar, it’s been a while since I’ve done any programming so I’m not sure. Have you tried it?

    Henry

    July 28, 2008 at 5:56 pm

  6. 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

    September 23, 2008 at 12:37 am

  7. Thanks Henry, this really helped!

    Dathan

    September 25, 2008 at 1:51 pm

  8. 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

    October 14, 2008 at 4:40 pm

  9. Thanks Henry, I was having a bit of a nightmare with this too. Works great

    Willie

    March 24, 2009 at 1:49 am

  10. Thanks this was a real timesaver.

    Kelly Ford

    August 25, 2009 at 11:38 am

  11. Do this in one line:

    list.RootFolder.SubFolders.Add(….)

    Ethan Deng

    August 28, 2009 at 9:00 am

  12. Any chance of a web service implementation example?

    MichaelL

    October 27, 2009 at 5:34 am

  13. nice article its very much helpful to me dear

    tutu

    January 4, 2010 at 9:24 pm

  14. has anyone figured a way to create a new folder using VBA? appreciate your help

    excel pro

    January 6, 2010 at 4:19 am

  15. What you are doing here is what I need to know how to create from scratch; I’m new to SP and all. I have to find away to create folders and or a document library with sub folders in it to that mirrors a current file folders on another application call plum tree so that I can migrate those documents in that application into SP. Any guidance would be great. Thank you in advance.

    Matthew

    January 7, 2010 at 12:05 pm

  16. Not to be the type to complain, your example is good, dont have a problem there, however all examples should always show exactly how to create something in a visual studio win form or console application. You have to remember that alot of people out there dont have 1/10th the knowledge you do, so in shareing knowledge always assume reader have no idea what you are talking about. Go step by step to get the exact some result, code snippets never work as most people never know how or where to place them so they always work.

    Tom

    January 21, 2010 at 9:37 am


Leave a Reply