top of page

Check/Retrieve Folder Operations On/From SharePoint Online Libraries Using PnP Core CSOM Library

Introduction

In this article, you will learn about checking/retrieving the folders on/from SharePoint Online libraries, using PnP Core CSOM library.


The main advantage of using PnP Core library 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 method, given below is used.

  • GetSharePointOnlineAuthenticatedContextToken

The parameters required are,

  • SharePoint Online site URL.

  • Tenant UserId.

  • Tenant Password (or secured string).


Check If Folder Exists

Using PnP Core CSOM library, we can check if the folder already exists on the site library. The steps involved are- 

  1. Input the site detail, the user details for the authentication, library and folder information.

  2. Authenticate and get the client context of the site.

  3. Retrieve the target list, using PnP Core library with the help of GetListByTitle method.

  4. Using the list object, access the root folder and then the required folder with FolderExists method.

  5. Display the results. 

The code snippet, given below shows the example of checking the folder availability.  

  1. // Input Parameters    

  2. string siteUrl = "https://nakkeerann.sharepoint.com/";  

  3. string userName = "abc@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context    

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context

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

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

  12.  {  

  13. // Input Parameter         

  14. string listName = "Documents";          

  15. string folderName = "TestFolder";  

  16. // Get Library         

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

  18. // Create Folder

  19. bool folderExists = list.RootFolder.FolderExists(folderName);  

  20. // Output

  21. if (folderExists)          

  22. {              

  23. Console.WriteLine("Folder exists");          

  24. }  

  25. else         

  26. {              

  27. Console.WriteLine("Folder doesn't exists");          

  28. }           

  29. Console.ReadKey();       

  30. }  

  31. }  

  32. catch (Exception ex)  

  33. {      

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

  35. Console.ReadKey();  

  36. }  


Retrieve Folder

The available folders can be retrieved from SharePoint libraries, using PnP Core CSOM library. The steps involved are - 

  • Input the site detail, the user details for the authentication, library and folder information.

  • Authenticate and get the client context of the site.

  • Retrieve the target list, using PnP Core library with the help of GetListByTitle method.

  • Using the list object, get the required folder, using ResolveSubFolder method.

  • Display the results.

The code snippet, given below shows the example of retrieving the folder/subfolder from the library.

  1. // Input Parameters    

  2. string siteUrl = "https://nakkeerann.sharepoint.com/";  

  3. string userName = "abc@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context    

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context

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

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

  12. {  

  13. // Input Parameter         

  14. string listName = "Documents";          

  15. string folderName = "TestFolder";  

  16. // Get Library         

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

  18. // Create Folder         

  19. Folder folder = list.RootFolder.ResolveSubFolder(folderName);  

  20. // Output         

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

  22. Console.ReadKey();       

  23. }  

  24. }  

  25. catch (Exception ex)  

  26. {      

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

  28. Console.ReadKey();  

  29. }  


Summary Thus, you have learned how to check if the folder exists on the library and also to retrieve the folder object from the library, using PnP Core CSOM library. The samples given above are compatible on SharePoint Online sites. For SharePoint On-Premise, the authentication method needs to be changed.

0 comments
bottom of page