SharePoint 2010 - Programmatically create a folder in document libraries and lists

Update: April 08, 2013 | Publication: March 21, 2011 | by Andreas

Maybe this was easy for everyone else but I have to admit that this wasn’t easy for me. As you may know I have developed a tool for SharePoint 2010 Site Provisioning in order to enhance the SharePoint development process which wasn’t able to programmatically create folders and subfolder in a document library or list.

So while I tried to change this I had a lot of trouble to achieve my goal.

Programmatically create a folder in a document library

This is the desired result:

Programmatically create a folder in a document library

I was able to successfully create a folder in a document library using this code:

SPList list = web.Lists.TryGetList("ListTitle");
SPFolderCollection folderColl = list.RootFolder.SubFolders;
SPFolder newFolder = folderColl.Add(FolderUrl);

FolderUrl means one of the following:

  • "/Document Library/Folder1"
    Creates a folder named “Folder1” under “Document Library”…
  • "/Document Library/Folder1/Folder11"
    Creates a folder named “Folder11” under “Folder1” in “Document Library”…
  • "/Subsite/Document Library/Folder1"
    Creates a folder named “Folder1” under “Document Library” in a sub site…
  • "/Subsite/Document Library/Folder1/Folder11"
    Creates a folder named “Folder11” under “Folder1” in “Document Library” in a sub site…

You need to use an URL including the name of the new folder which is different to creating a folder in a list.

Programmatically create a folder in a list

So after we can create a folder in a SharePoint document library we now can create them in SharePoint lists.

This is the desired result:

Programmatically create a folder in a list

I was able to successfully create a folder in a SharePoint list using this code:

SPList list = web.Lists.TryGetList("ListTitle");

SPListItem newFolderItem = list.Items.Add(completeFolderUrl, SPFileSystemObjectType.Folder);
newFolderItem["Title"] = "TitleOfTheFolder";
newFolderItem.Update();

completeFolderUrl means one of the following:

  • "/lists/Custom List"
    The folder is created under “Custom List”…
  • "/lists/Custom List/Folder1"
    The folder is created under “Folder1” in “Custom List”…
  • "/aboutus/lists/Custom List"
    The folder is created under “Custom List” in a sub site…
  • "/aboutus/lists/Custom List/Folder1"
    The folder is created under “Folder1” in “Custom List” in a sub site…

Important: The folder didn’t create for me when my “completeFolderUrl” ended with a slash ‘/’.

Summary

I don’t know if I’m the only one who had problems to programmatically create folders and subfolder in a document library or list but I hope this helps someone else before you bang your head against the wall or table. Winking smile

Hope you like it...


Get FREE & Advanced SharePoint Training, how-to's, tips & tricks:
Learn SharePoint

Comments (7) -

3/21/2011 7:12:40 PM #

Aryan Nava

Excellent post and very useful.
Thank You

Aryan Nava Canada Reply

9/14/2011 4:06:44 PM #

Sebastian

hello, i really need to get this working, can you tell me where to put this code?
thanks in advance.

Sebastian Argentina Reply

9/15/2011 10:04:56 PM #

Andreas Glaser

Hi Sebastian,

depends on what you want to do... you can put in a console application, a SharePoint web part or a forms application.

Can you please add more information?

Thanks
Andreas

Andreas Glaser Switzerland Reply

10/18/2011 8:52:44 PM #

Leon Tunctson


How can I programmically add an alert to a subfolder that is under the "Shared Documents" folder?

Leon Tunctson United States Reply

12/9/2011 4:00:17 PM #

Aditya Reddy

Thanks for your post.

Aditya Reddy India Reply

8/7/2012 6:40:48 PM #

lama

Hi Andreas,

How can I copy only folders andtheir subfolders from one doclib to another. If the files are included in any of the folder(s), then don't copy them. copy only folder structure.

Thanks,
Lama

lama United States Reply

8/10/2012 8:26:23 AM #

Andreas Glaser

Hi Lama,

haven't tried it yet... but I guess there must be a possibility. Does anybody know?

Andreas

Andreas Glaser Switzerland Reply

Pingbacks and trackbacks (1)+

Add comment


Loading