Create Folders and Sub-Folders in SharePoint Programmatically

How to create Folders in SharePoint?

To create a folder in SharePoint list or Library, follow these steps:

  • Navigate to your SharePoint list or library
  • From the “Files” tab on the Ribbon, click on the “New Folder” buttonsharepoint 2013 create a folder
  • Give a name to your new folder and click “Save” how to create folder in sharepoint 2013 programmatically

What if you don’t find the “New Folder” button in the ribbon? 
If you don’t get the “New Folder” button, it could be disabled. Here is how to enable folders in the SharePoint list:

  • Go to List settings, Click on “Advanced Settings”
  • Scroll down and set “Yes” to Make “New Folder” command available option.sharepoint 2010 create folder in a list

In a Migration automation tool development, had to create folders and sub-folders from the code programmatically. Here I’m sharing the code snippets in C#

using (SPSite site = new SPSite("https://sharepoint.company.com"))
{
    using (SPWeb web = site.OpenWeb())
    {
         //Get the List
         SPList list = web.Lists["Project Tracker"];

         //Create a Folder
         SPListItem folder = list.AddItem(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "Sales Documents");
         folder.Update();

         //Create a Sub-Folder
         SPListItem SubFolder = list.AddItem(folder.Folder.ServerRelativeUrl , SPFileSystemObjectType.Folder, "APAC Sales Docs");
         SubFolder.Update();

     }         
}

Alternatively, you can use:

//Get the List
SPList list = web.Lists["Shared Documents"];
String url = list.RootFolder.ServerRelativeUrl.ToString();
SPFolderCollection folders = web.GetFolder(url).SubFolders;

//Create new folder
folders.Add("New Documents");

Here is the PowerShell to create a folder in SharePoint: How to Create a Folder in SharePoint Document Library using PowerShell?

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

6 thoughts on “Create Folders and Sub-Folders in SharePoint Programmatically

  • It gave me an error “Could not load file or assembly ‘Microsoft.SharePoint.Library, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ or one of its dependencies. The system cannot find the file specified.”:”Microsoft.SharePoint.Library, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c

    Reply
  • Will you solution to create a folder work with SharePoint Online or is this only for on premise?

    Reply
  • That’s give me an error, this is what i get :

    Le terme « Get-SPWeb » n’est pas reconnu comme nom d’applet de commande, fonction, fichier de script ou programme exécutable. Vérifiez l’orthographe du nom, ou si un chemin d’accès ex
    iste, vérifiez que le chemin d’accès est correct et réessayez.
    Au niveau de ligne : 4 Caractère : 15
    + $web=Get-SPWeb <<<< "https://sharepoint.company.com" + CategoryInfo : ObjectNotFound: (Get-SPWeb:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Vous ne pouvez pas appeler de méthode sur une expression ayant la valeur Null. Au niveau de ligne : 6 Caractère : 28 + $list=$web.Lists.TryGetList <<<< ("Documents") + CategoryInfo : InvalidOperation: (TryGetList:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *