Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting – HostForLIFE.eu :: How to Check/Retrieve Folder Operations On/From SharePoint Online Libraries Using PnP Core CSOM Library?
In this article, I will tell you about how to check/retrieve 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.
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.
1 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
// 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 bool folderExists = list.RootFolder.FolderExists(folderName); // Output if (folderExists) { Console.WriteLine("Folder exists"); } else { Console.WriteLine("Folder doesn't exists"); } Console.ReadKey(); } } catch (Exception ex) { Console.WriteLine("Error Message: " + ex.Message); Console.ReadKey(); } |
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
// 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.ResolveSubFolder(folderName); // Output Console.WriteLine("Server Relative Path : " + folder.ServerRelativeUrl); Console.ReadKey(); } } catch (Exception ex) { Console.WriteLine("Error Message: " + ex.Message); Console.ReadKey(); } |
I hope it works for you! Thank you.
SharePoint 2013 Hosting Recommendation
HostForLIFE.eu’s SharePoint 2013 Hosting solution offers a comprehensive feature set that is easy-to-use for new users, yet powerful enough for the most demanding web developer expert. Hosted SharePoint Foundation 2013 is the premiere web-based collaboration and productivity enhancement tool on the market today. With SharePoint 2013 Foundation, you can quickly access and manage documents and information anytime, anywhere though a Web browser in a secure and user friendly way. SharePoint hosting services start at only at €9.99/mo, allowing you to take advantage of the robust feature set for a small business price. HostForLIFE.eu offers a variety of hosted SharePoint Foundation 2013 plans as well as dedicated SharePoint 2013 Foundation options.
Print article | This entry was posted by Peter on June 8, 2017 at 4:39 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |