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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Security;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- namespace CheckListItem
- {
- class Program
- {
- static void Main(string[] args)
- {
- string userName = "[email protected]";
- string siteURL = "https://c986.sharepoint.com/sites/Vijai";
- Console.WriteLine("Enter your password.");
- SecureString password = GetPassword();
- // ClienContext - Get the context for the SharePoint Online Site
- using (var clientContext = new ClientContext(siteURL))
- {
- // SharePoint Online Credentials
- clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
- // Get the SharePoint web
- Web web = clientContext.Web;
- // Get the list by name
- List list = web.Lists.GetByTitle("Documents");
- // Get the list item by ID
- ListItem item = list.GetItemById(17);
- // Load the site group properties
- clientContext.Load(item);
- // Execute the query to the server.
- clientContext.ExecuteQuery();
- // Check whether the list item is a file or a list folder.
- if (item.FileSystemObjectType == FileSystemObjectType.File)
- {
- Console.WriteLine("List item is a file.");
- }
- Console.ReadLine();
- }
- }
- private static SecureString GetPassword()
- {
- ConsoleKeyInfo info;
- //Get the user's password as a SecureString
- SecureString securePassword = new SecureString();
- do
- {
- info = Console.ReadKey(true);
- if (info.Key != ConsoleKey.Enter)
- {
- securePassword.AppendChar(info.KeyChar);
- }
- }
- while (info.Key != ConsoleKey.Enter);
- return securePassword;
- }
- }
- }

Shamim UddinPosted Oct 19, 2016, 10:29 AM
Nice