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.
hey ma, please see here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.aspx
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….
How could this be done in VB6 or VBA?
Just curious – doesn’t this work?
list.RootFolder.SubFolders.Add(”Huh”);
Hey Oskar, it’s been a while since I’ve done any programming so I’m not sure. Have you tried it?
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”));
Thanks Henry, this really helped!
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
Thanks Henry, I was having a bit of a nightmare with this too. Works great
Thanks this was a real timesaver.
Do this in one line:
list.RootFolder.SubFolders.Add(….)
Any chance of a web service implementation example?
nice article its very much helpful to me dear
has anyone figured a way to create a new folder using VBA? appreciate your help
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.
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.
Hi,
I need to move files to a destination in the library depending upon selection of parameters in the edit form. For ex., on selection of two mandatory fields A and B, file will be uploaded to /Root/A/B.
How can we code for the editform.aspx form?
Can someone plz explain.
Thanx!!!
Here is an idea, why not for one time just does the solution for what the question was asked, from the smallest step to the end result. Showing each step from creating the VS solution to the actually deployment into the environment. It seems that not one person every answers the questions that way they always give code snips. Well the first part of the question asked was hello I’m new to Sharepoint, so why don’t you help the guy out walk through the whole solution. Remember community is supposed to help.
i really need help with sharepoint customization! i understand the code above but how do i call it to use it in the item added event?
so many thanks
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.
Hi John, it’s not possible to create document libraries within document libraries. You might want to look at the Document Sets feature of SharePoint Server 2010 Std+ in order to attach different automatic workflows to different types of documents.
is it possible to create folder in share point via VBA(Macro)
Check out this link too its also having a wonderful explanation about Create a Document in SharePoint and add folder in SharePoint Document using C#….
http://mindstick.com/Articles/19b8cf75-6d2e-4504-8840-718717bd53ba/?Create%20a%20Document%20in%20SharePoint%20and%20add%20folder%20in%20SharePoint%20Document%20using%20C#
Did somebody found a solution for VBA? That would be very helpful !
Thank you 🙂
is it possible to create folder in share point via VBA(Macro)