Sunday, October 21, 2012

Create Folders and Sub-Folders in SharePoint Programmatically

How to create Folders in SharePoint 2013:
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 "New Folder" buttonsharepoint 2013 create a folder
  • Give a name to your new folder and click "Save" sharepoint 2013 create folder
What if you don't find "New Folder" button in the ribbon? 
If you don't get "New Folder" button, it could be disabled. Here is how to enable folders in 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# and in PowerShell.
      using (SPSite site = new SPSite(""))
                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");

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

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");
The above codes doesn't provide much fault tolerance, isn't it?
  • What if the particular library we are looking for doesn't exists?
  • What if the folder we are trying to add already exists on the library?
Error! So lets re-write the code in PowerShell with little fault tolerance!

How to Create Folders and Sub-Folders in SharePoint List using PowerShell?

Here is the PowerShell code snippet for creating Folders and Sub-Folders inside SharePoint List or Library:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the Web
$web=Get-SPWeb ""
#Get the List/Library

if($list)  #Check If List exists!
  # Create a Folder "Sales Documents"
  # Check Folder Doesn't exists in the Library!
  $folder = $list.ParentWeb.GetFolder($list.RootFolder.Url + "/" +"Sales List");

  if ($folder.Exists -eq $false)
    #Create a Folder
    $folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "Sales Sub-Item")
  #Create a Sub-Folder "APAC Sales Documents" inside "Sales Documents"
  #Check if sub-folder doesn't exists already
  $Subfolder = $list.ParentWeb.GetFolder($folder.URL + "/" + "APAC Sales Documents");
  if ($Subfolder.Exists -eq $false)
    #Create a Sub-Folder Inside "Sales Documents"
    $Subfolder = $list.AddItem($folder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "APAC Sales Documents")
Its just converted from .Net Object model code with some additional error tolerance.

How to Create Folders in SharePoint Library:
To create folders inside Document libraries, Use:
    $folder = $list.Folders.Add("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $FolderName)

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:


  1. 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 <<<< ""
    + 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

    1. 1. Are you trying to Run this PowerShell script on SharePoint 2007? If so, for Cmdlets like Get-SPWeb, you have to follow How to use PowerShell with MOSS 2007
      2. Make sure the parameters: URL "" and the list "Documents" are valid.


Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...