top of page

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 = "abc@nakkeerann.onmicrosoft.com";      

  6. string password = "***";  

  7. // PnP component to set context         

  8. AuthenticationManager authManager = new AuthenticationManager();  

  9. try     

  10. {  

  11. // Get and set the client context

  12. // Connects to SharePoint online site using inputs provided

  13. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))          

  14. {  

  15. // Input Parameter             

  16. string listName = "Documents";              

  17. string folderName = "TestFolder";  

  18. // Get Library             

  19. List list = clientContext.Site.RootWeb.GetListByTitle(listName);  

  20. // Create Folder             

  21. Folder folder = list.RootFolder.CreateFolder(folderName);  

  22. // Output             

  23. Console.WriteLine("New " + folder. Name + " folder is created");              

  24. Console.ReadKey();           

  25. }      

  26. }  

  27. catch (Exception ex)      

  28. {          

  29. Console.WriteLine("Error Message: " + ex.Message);          

  30. Console.ReadKey();      

  31. }  

  32. }  

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 = "abc@nakkeerann.onmicrosoft.com";      

  6. string password = "***";  

  7. // PnP component to set context         

  8. AuthenticationManager authManager = new AuthenticationManager();  

  9. try     

  10. {  

  11. // Get and set the client context

  12. // Connects to SharePoint online site using inputs provided

  13. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))          

  14. {  

  15. // Input Parameter             

  16. string listName = "Documents";              

  17. string folderName = "TestFolder2";  

  18. // Get Library             

  19. List list = clientContext.Site.RootWeb.GetListByTitle(listName);  

  20. // Retrieves and Creates Folder if not exists             

  21. Folder folder = list.RootFolder.EnsureFolder(folderName);  

  22. // Output             

  23. Console.WriteLine("Folder Name : " + folder. Name);              

  24. Console.WriteLine("Server Relative Path : " + folder.ServerRelativeUrl);              

  25. Console.ReadKey();          

  26. }      

  27. }  

  28. catch (Exception ex)      

  29. {          

  30. Console.WriteLine("Error Message: " + ex.Message);          

  31. Console.ReadKey();      

  32. }  

  33. }  

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.

0 comments

Comments


bottom of page