SharePoint Online Create Folder Operations, Using PnP Core CSOM Library

Introduction
 
In this article, you will learn about creating folders on SharePoint Online libraries, using PnP Core CSOM library.
 
The main advantage of using PnP Core libraries is the reduced code to get the required information. The required object can be retrieved with a very small piece of code, once the client context is set.
 
Prerequisite
  • PnP Core CSOM documentation can be found on the office site here.
  • PnP Core CSOM library packages can be downloaded here
The code, given below, is being tested using Visual Studio console application. Once the console application is created, the packages can be installed using "Install-Package SharePointPnPCoreOnline" command on Package Manager console of Visual Studio. Once installed, the references and packages will be imported to the solution.
 
The references used in the sample are given below.
  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core 
Connect to SharePoint online site
 
The Authentication Manager is used to retrieve the client context of the site. To connect to SharePoint Online site, the below method is used.
  • GetSharePointOnlineAuthenticatedContextToken 
The parameters required are,
  • SharePoint Online site URL
  • Tenant UserId
  • Tenant Password (or secured string)
Create Folder
 
The folders can be created using PnP Core library. The steps involved are given below.
  • Input the site detail, user details for authentication, library and folder information.
  • Authenticate and get the client context of the site.
  • Using the web object, get the target library. From the library object, access the root folder (or target folder). Then, using folder object, create a new folder named as "input".  
The following code snippet shows the folder creation operation sample.
  1. private static void CreateFolder()  
  2. {  
  3.     // Input Parameters      
  4.     string siteUrl = "https://nakkeerann.sharepoint.com/";  
  5.     string userName = "[email protected]";  
  6.     string password = "***";  
  7.       
  8.     // PnP component to set context      
  9.     AuthenticationManager authManager = new AuthenticationManager();  
  10.     try  
  11.     {  
  12.         // Get and set the client context  
  13.         // Connects to SharePoint online site using inputs provided  
  14.         using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  15.         {  
  16.   
  17.             // Input Parameter  
  18.             string listName = "Documents";  
  19.             string folderName = "TestFolder";  
  20.             // Get Library  
  21.             List list = clientContext.Site.RootWeb.GetListByTitle(listName);  
  22.             // Create Folder  
  23.             Folder folder = list.RootFolder.CreateFolder(folderName);  
  24.   
  25.             // Output  
  26.             Console.WriteLine("New " + folder.Name + " folder is created");  
  27.             Console.ReadKey();  
  28.   
  29.         }  
  30.     }  
  31.     catch (Exception ex)  
  32.     {  
  33.         Console.WriteLine("Error Message: " + ex.Message);  
  34.         Console.ReadKey();  
  35.     }  
  36. }   
Ensure Folder
 
Before creating folders, check if the folder exists. If not, create the folder. The same logic can be implemented on single call execution. The operation ensures the folder at the target path. The steps involved are -
  • Input the site detail, user details for authentication, library and folder information.
  • Authenticate and get the client context of the site.
  • Using the web object, get the target library. From the library object, access the root folder (or target folder). Then using folder object, check and create new folder with folder name as input. EnsureFolder method is used for this operation. 
The following code snippet shows the folder creation operation sample. 
  1. private static void EnsureFolder()  
  2. {  
  3.     // Input Parameters      
  4.     string siteUrl = "https://nakkeerann.sharepoint.com/";  
  5.     string userName = "[email protected]";  
  6.     string password = "***";  
  7.   
  8.     // PnP component to set context      
  9.     AuthenticationManager authManager = new AuthenticationManager();  
  10.     try  
  11.     {  
  12.         // Get and set the client context  
  13.         // Connects to SharePoint online site using inputs provided  
  14.         using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  15.         {  
  16.             // Input Parameter  
  17.             string listName = "Documents";  
  18.             string folderName = "TestFolder2";  
  19.             // Get Library  
  20.             List list = clientContext.Site.RootWeb.GetListByTitle(listName);  
  21.   
  22.             // Retrieves and Creates Folder if not exists  
  23.             Folder folder = list.RootFolder.EnsureFolder(folderName);  
  24.   
  25.             // Output  
  26.             Console.WriteLine("Folder Name : " + folder.Name);  
  27.             Console.WriteLine("Server Relative Path : " + folder.ServerRelativeUrl);  
  28.             Console.ReadKey();  
  29.         }  
  30.     }  
  31.     catch (Exception ex)  
  32.     {  
  33.         Console.WriteLine("Error Message: " + ex.Message);  
  34.         Console.ReadKey();  
  35.     }  
  36. }  
The following snapshot shows the folders created in the library.

 
Summary
 
Thus, you have learned how to create folders on the SharePoint libraries, using PnP Core CSOM library. Also, we have seen one more example of creating a folder by checking if it doesn't exist.