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
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()
{
// Input Parameters
string siteUrl = "https://nakkeerann.sharepoint.com/";
string userName = "abc@nakkeerann.onmicrosoft.com";
string password = "***";
// PnP component to set context
AuthenticationManager authManager = new AuthenticationManager();
try
{
// Get and set the client context
// Connects to SharePoint online site using inputs provided
using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
{
// Input Parameter
string listName = "Documents";
string folderName = "TestFolder";
// Get Library
List list = clientContext.Site.RootWeb.GetListByTitle(listName);
// Create Folder
Folder folder = list.RootFolder.CreateFolder(folderName);
// Output
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()
{
// Input Parameters
string siteUrl = "https://nakkeerann.sharepoint.com/";
string userName = "abc@nakkeerann.onmicrosoft.com";
string password = "***";
// PnP component to set context
AuthenticationManager authManager = new AuthenticationManager();
try
{
// Get and set the client context
// Connects to SharePoint online site using inputs provided
using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
{
// Input Parameter
string listName = "Documents";
string folderName = "TestFolder2";
// Get Library
List list = clientContext.Site.RootWeb.GetListByTitle(listName);
// Retrieves and Creates Folder if not exists
Folder folder = list.RootFolder.EnsureFolder(folderName);
// Output
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.
Comments