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

SharePoint – Add folders to Document Library programmatically

You might be interested in:

Discussion

22 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
  8. Where do I write this code? I mean in a web part or ?

    Posted by Shobha | November 4, 2010, 4:39 am
  9. Shobha, you can add this code anywhere as far as you have access to “site” object.

    I used it on Feature Activation.

    -Praveen

    Posted by Praveen Modi | November 10, 2010, 8:47 am
  10. Thanks Praveen, I have tried it works fine.

    Posted by Shobha | November 29, 2010, 7:32 am
  11. Is it possible to create a document library within another document library? I would like to attached different automatic workflows to different document libraries within a main document library.

    Is this possible thur GUI or programmatically?

    Thanks,

    John.

    Posted by John | March 2, 2011, 6:38 pm
  12. John,

    SharePoint doesn’t allow to create document library within another document library. However you can associate multiple content types to a document library and each content type can have a unique workflow attached to it.

    You can also create a View for each content type which will give you the similar feel as having document library within another document library.

    Hope this helps!

    Cheers,
    Praveen

    Posted by Praveen | March 2, 2011, 9:45 pm
  13. Praveen,

    Is there a tool to build more than one wsp files as setup.exe. We need to install share point solution include web parts, lists, custom content types etc. We need build installer as exe file to do all in one short. Plese suggest me any tools available in achieving this.

    Regards

    Posted by Venkatesh | April 19, 2011, 3:31 am
  14. Venkatesh,

    I am not aware about the tool to build the installer.

    However I would create a batch files which deploys multiple WSP’s. I think it much easier approach to achieve the same objective.

    Cheers,
    Praveen

    Posted by Praveen | April 19, 2011, 8:12 pm
  15. Hi Praveen,
    Any thoughts on how I would go about creating a folder as part of a workflow (using SPD)? – Ideally the folder would be on a Document Library on a SubSite.
    Thanks in advance!
    Jon B

    Posted by Jon B | June 30, 2011, 8:04 am
  16. Praveen,

    I’m using macros in word and trying to create a folder in sharepoint if it’s not found. The macro I currently have uses bookmarks in the document and works in my local desktop but will not work in sharepoint (Dir, MkDir).

    I understand that they are different programs and some vb languages are different. Most macros reference the address where to look for something. In your example I don’t see the address referenced. New to Sharepoint so just now learning to navigate through it

    Would be greatful for any help.

    Thanks,
    Gino

    Thanks

    Posted by Gino V Perez | September 27, 2011, 6:44 pm
  17. [...] SharePoint – Add folders to Document Library programmatically [...]

    Posted by links Sharepoint webservices « Just tinkering Blog | November 17, 2011, 4:02 pm
  18. Check this helpful link….

    http://mindstick.com/Articles/19b8cf75-6d2e-4504-8840-718717bd53ba/?Create%20a%20Document%20in%20SharePoint%20and%20add%20folder%20in%20SharePoint%20Document%20using%20C#

    Its also having with wonderful explanation on Create a Document in SharePoint and add folder in SharePoint Document using C#.

    Posted by Tanuj Kumar | December 20, 2011, 1:54 am
  19. Hello Praveen
    I apologize if this is a reduntant question. I have a List, when the list is created I want it to create a folder in a document library using a couple of the column names in the original list. I am using Designer 2007. Any help is appreciated.

    Posted by D Baldassarri | January 17, 2012, 7:24 pm
  20. How Can i add folder in document library using document object model

    Posted by Kiran Kumar | May 10, 2012, 6:11 am
  21. Many thanks to people who willingly share what it takes to make life online a little easier. You have no idea how much this is appreciated.

    Posted by William Young | June 6, 2012, 1:25 am
  22. Hi Praveen,

    Thanks for the post.It helped me very much.

    Posted by pradeep | September 4, 2013, 4:32 am

Post a comment

WP-SpamFree by Pole Position Marketing

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

Recent Comments

  • Tyler Canton: I can’t see my component once I select it form the edit mode and then after I drag into my...
  • Praveen Modi: Anand – glad that the issue is resolved. I have also updated the article as other people might...
  • Anand: Hi Praveen Thanks for your response. You were right I had enabled only port 22 and 80 and trying to access AEM...
  • Praveen Modi: Anand, Try $curl http://localhost:4502/ on the server ans see if you receive a response. If you get a...
  • anand: Hi Followed your steps and completed installation on was ec2. Trying to access author using...