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