How To Check Whether The List Item Is A File Or A List Folder In Sharepoint Online Using CSOM

In this blog, you will see whether the list item is a file or a list folder in SharePoint Online, using CSOM. Please refer to my previous article Connect To SharePoint 2013 Online Using CSOM With Console Application.

Code Snippet

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Security;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. using Microsoft.SharePoint.Client;  
  9.   
  10. namespace CheckListItem
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             string userName = "[email protected]";  
  17.             string siteURL = "https://c986.sharepoint.com/sites/Vijai";              
  18.             Console.WriteLine("Enter your password.");  
  19.             SecureString password = GetPassword();  
  20.   
  21.             // ClienContext - Get the context for the SharePoint Online Site               
  22.             using (var clientContext = new ClientContext(siteURL))  
  23.             {  
  24.                 // SharePoint Online Credentials    
  25.                 clientContext.Credentials = new SharePointOnlineCredentials(userName, password);  
  26.   
  27.                 // Get the SharePoint web    
  28.                 Web web = clientContext.Web;  
  29.   
  30.                 // Get the list by name  
  31.                 List list = web.Lists.GetByTitle("Documents");  
  32.   
  33.                 // Get the list item by ID  
  34.                 ListItem item = list.GetItemById(17);  
  35.   
  36.                 // Load the site group properties    
  37.                 clientContext.Load(item);  
  38.   
  39.                 // Execute the query to the server.    
  40.                 clientContext.ExecuteQuery();  
  41.   
  42.                 // Check whether the list item is a file or a list folder.  
  43.                 if (item.FileSystemObjectType == FileSystemObjectType.File)  
  44.                 {  
  45.                     Console.WriteLine("List item is a file.");  
  46.                 }  
  47.   
  48.                 Console.ReadLine();  
  49.             }  
  50.         }  
  51.   
  52.         private static SecureString GetPassword()  
  53.         {  
  54.             ConsoleKeyInfo info;  
  55.             //Get the user's password as a SecureString    
  56.             SecureString securePassword = new SecureString();  
  57.             do  
  58.             {  
  59.                 info = Console.ReadKey(true);  
  60.                 if (info.Key != ConsoleKey.Enter)  
  61.                 {  
  62.                     securePassword.AppendChar(info.KeyChar);  
  63.                 }  
  64.             }  
  65.             while (info.Key != ConsoleKey.Enter);  
  66.             return securePassword;  
  67.         }  
  68.     }  
  69. }