// you’re reading...

SharePoint

SharePoint - Add folders to Document Library programmatically

Today I had a requirement to add folders programmatically to the default ‘Site Collection Images’ document library. Its quite simple and can easily be achieved with few lines of code. Here is a method of how to do that:

//Remember Document library is also a list in SharePoint
String url = site.Lists["Site Collection Images"].RootFolder.ServerRelativeUrl.ToString();
SPFolderCollection folders = site.GetFolder(url).SubFolders; 

//Create new folder
folders.Add("Newfolder");

You can use the same code to create folders/subfolders in any custom document library as well.

You might be interested in:

Discussion

7 comments for “SharePoint - Add folders to Document Library programmatically”

  1. Thanks for the post, i need to know how to add folder to “Site collection images” from specific conetnt type associated already to the Library, suppose we add special kind of conetnt type “Galery Folder” .. so how to add new folder to the Images Library.

    Posted by Basel | May 23, 2009, 11:48 pm
  2. Bingo !!! i was loking 4 exact thing.
    tanX pro

    Posted by hd | June 23, 2009, 4:20 am
  3. Thank you, sorry to ask, but would i add code to have this function automated.. Basically I was to insure that folders A B C D E are always created in this specific portal, when new children portals are created.

    Posted by Robert Woodlock | August 10, 2009, 9:22 am
  4. [...] to the sharepoint libararies, list etc..but I like the kind of approach praveen takes in this post. I am using the same with no issues here is the code which I am [...]

    Posted by Create Folder Programmatically | My Tech Life Everyday | October 20, 2009, 8:18 am
  5. couldn’t you just do:
    SPFolderCollection collection = site.Lists["Site Collection Images"].RootFolder.SubFolders;
    ?

    Posted by Juan Larios | November 5, 2009, 3:37 pm
  6. Juan, seems like it should work but I haven’t tried it. I will give it a try and will update the post accordingly.

    Thanks for visiting my blog.

    -Praveen

    Posted by Praveen Modi | November 5, 2009, 10:21 pm
  7. Hi!

    I tried your code, it works great. However, I don’t see the created folder in explorer view. How come?

    Regards,

    Maarten

    Posted by Maarten | November 17, 2009, 7:34 am

Post a comment

Spam Protection by WP-SpamFree Plugin

    Locations of visitors to this page
View Praveen Modi's profile on LinkedIn

Recent Comments

  • fred: The above mentioned tips are helpful. However, by the use of a third-party caching solution for SharePoint you...
  • Ramiz Uddin: Praveen, I would need your help. I’m new on sharepoint and very much confused with its deployment...
  • Maarten: Hi! I tried your code, it works great. However, I don’t see the created folder in explorer view. How...
  • Praveen Modi: Juan, seems like it should work but I haven’t tried it. I will give it a try and will update the...
  • Juan Larios: couldn’t you just do: SPFolderCollection collection = site.Lists["Site Collection...