
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-
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, access the root folder and then the required folder with FolderExists method.
Display the results.
The code snippet, given below shows the example of checking the folder availability.
// Input Parameters