Musings on SharePoint, Adobe CQ, ECM, and more…

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.

Praveen Modi

Praveen works at Razorfish which is one of the largest digital advertising agency in the world. His mantra for life is "You are never too old to set another goal or to dream a new dream". He lives in sunny Austin, TX with his beautiful wife Nidhi and son Aariv.

23 Comments

  1. Basel

    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.

  2. hd

    Bingo !!! i was loking 4 exact thing.
    tanX pro

  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.

  4. Juan Larios

    couldn’t you just do:
    SPFolderCollection collection = site.Lists[“Site Collection Images”].RootFolder.SubFolders;
    ?

  5. Praveen Modi

    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

  6. Maarten

    Hi!

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

    Regards,

    Maarten

  7. Shobha

    Where do I write this code? I mean in a web part or ?

  8. Praveen Modi

    Shobha, you can add this code anywhere as far as you have access to “site” object.

    I used it on Feature Activation.

    -Praveen

  9. Shobha

    Thanks Praveen, I have tried it works fine.

  10. John

    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.

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

  12. Venkatesh

    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

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

  14. Jon B

    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

  15. Gino V Perez

    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

  16. Tanuj Kumar

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

  17. D Baldassarri

    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.

  18. Kiran Kumar

    How Can i add folder in document library using document object model

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

  20. pradeep

    Hi Praveen,

    Thanks for the post.It helped me very much.

  21. Jeremy

    I am a complete noob, so here is my stupid question: where do i put this code? does is go in a data connection? or is it a file? what is the extension? how do i fire it off?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.