Create Folder and Subfolder in SharePoint Document Library using PnP Core CSOM Library

In this SharePoint PnP tutorial, we will learn how to create folders and subfolders inside SharePoint Document library Programmatically using PnP Core CSOM Library. We will also see, if a folder exists inside a SharePoint document library programmatically using PnP.

If you are new to sharepoint PnP, following this article to know about PnP SharePoint Online Development using Patterns and Practices (PnP and Hide SharePoint List Title Column Programmatically using PnP CSOM.

Here, I got a requirement to create a folder and a subfolder inside a SharePoint Online document library programmatically. Here is my document library. I will create folder and Subfolder (Inside the folder) in “Documents” library.

create folder in sharepoint document library
create folder in sharepoint document library

Create Folder in Documents Library using SharePoint PnP Core CSOM Library

The below references we have to use in the code to work with PnP libraries.

  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core

Following functions are used for creating folder in SharePoint Library.

  • FolderExists() function can be used to check folder exists or not in the SharePoint List/Document library.
  • CreateFolder() function is used to create a new folder in the Sharepoint list or document library.

Here I have created an asp.net application and I have added a button.On click on the button, Creating a folder and subfolder in Sharepoint Documents library using PnP core CSOM library.

The given below code create a folder and subfolder(inside the folder) in “Documents” Library.

AuthenticationManager authMgr = new AuthenticationManager();
string siteURL = "https://domainName.sharepoint.com/sites/TsInfoPNP";
string userName = "****@domainName.onmicrosoft.com";
string password = "*****";
protected void btnCreateFolder_Click(object sender, EventArgs e)
{
			createFolder();
}
public void createFolder()
{

	string listname = "Documents";
	string folderName = "Departments";
	try
	{
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteURL, userName, password))
	{
	ctx.Load(ctx.Web);
	ctx.ExecuteQueryRetry();
	List list = ctx.Web.Lists.GetByTitle(listname);
	bool folderExists = list.RootFolder.FolderExists(folderName);
	if (!folderExists)
	{			 
Folder folder = list.RootFolder.CreateFolder(folderName).CreateFolder("IT");
	ctx.Load(folder);
	ctx.ExecuteQueryRetry();
	lblCreateFolder.Text = "Folder and SubFolder Created Successfully";
	}
	else
	{
	lblCreateFolder.Text = "Folder already exists";
	}
     }				
   }
catch (Exception ex) {

lblCreateFolder.Text = "Problem in creating folders in Documents library";	
			}
}

Run your application, click on Button “Create Folder in Documents Library”.

create folder in sharepoint document library
create folder in sharepoint document library

The below snapshot shows the folder “Departments” created in “Documents” library.

create folder in sharepoint list using pnp
create folder in sharepoint list using pnp

The below snapshot shows sub folder created inside the folder “Departments”.

sharepoint online create folder using pnp core csom
sharepoint online create folder using pnp core csom

Create Folder and Sub Folder inside the Master Page Gallery in SharePoint using PnP

we can create folder and subfolders inside the SharePoint master page gallery also. You just need to change the name of the list here like below:

string listname = "Master Page Gallery";

You may like following SharePoint folder tutorials :

In this article, we learned about how to Check the folder exist or not, create a folder in the SharePoint document library and create a suborder (inside the folder) using SharePoint PnP core CSOM Library.

We also saw how can we create folders and sub folders inside SharePoint Online master page gallery programmatically using PnP Core CSOM library.

>