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.
- private static void CreateFolder()
- {
-
- string siteUrl = "https://nakkeerann.sharepoint.com/";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
-
- string listName = "Documents";
- string folderName = "TestFolder";
-
- List list = clientContext.Site.RootWeb.GetListByTitle(listName);
-
- Folder folder = list.RootFolder.CreateFolder(folderName);
-
-
- Console.WriteLine("New " + folder.Name + " folder is created");
- Console.ReadKey();
-
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
- }
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.
- private static void EnsureFolder()
- {
-
- string siteUrl = "https://nakkeerann.sharepoint.com/";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
- string listName = "Documents";
- string folderName = "TestFolder2";
-
- List list = clientContext.Site.RootWeb.GetListByTitle(listName);
-
-
- Folder folder = list.RootFolder.EnsureFolder(folderName);
-
-
- Console.WriteLine("Folder Name : " + folder.Name);
- Console.WriteLine("Server Relative Path : " + folder.ServerRelativeUrl);
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
- }
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.