How to Get All the Files within a Folder using CAML Query in SharePoint Office 365 using C# CSOM

Sathish Nadarajan
 
Solution Architect
July 11, 2017
 
Rate this article
 
Views
46087

Recently met with a requirement to get all the Files alone from a Folder using CAML Query. Thought of sharing the small piece of code to the community and make sure that whether to use RECURSIVE and RECURSIVEALL.

Basically, the requirement is as below.

clip_image001

By Executing a CAML Query, I want to get the Three Files (File1, File2, File3). The below Simple CAML Query will retrieve only three files from the Folder Folder1

 using Microsoft.SharePoint.Client;
  
 
 namespace Office365.Console
 {
     class Program
     {
         static void Main(string[] args)
         {
             GetFiles();
         }
 
         public static void GetFiles()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string sourceSiteUrl = "https://*********.sharepoint.com/sites/RecordCentre";
 
             string userName = "Sathish@*********.onmicrosoft.com";
             string password = "*********";
 
             var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(sourceSiteUrl, userName, password);
 
             Web web = clientContext.Web;
             clientContext.Load(web);
             clientContext.Load(web.Lists);
             clientContext.Load(web, wb => wb.ServerRelativeUrl);
             clientContext.ExecuteQuery();
 
             List list = web.Lists.GetByTitle("MyRecordLibrary");
             clientContext.Load(list);
             clientContext.ExecuteQuery();
 
             Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/MyRecordLibrary/Folder1/");
             clientContext.Load(folder);
             clientContext.ExecuteQuery();
 
             CamlQuery camlQuery = new CamlQuery();
             camlQuery.ViewXml = @"<View Scope='Recursive'>
                                     <Query>
                                     </Query>
                                 </View>";
             camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
             ListItemCollection listItems = list.GetItems(camlQuery);
             clientContext.Load(listItems);
             clientContext.ExecuteQuery();
 
         }
 
     }
 }
 
 

In some scenario, we may need to get the folders as well. In that case, a small change on the CAML query will retrieve 5 items. (3 Files and two Folders Folder1.1, Folder1.1.1)

 camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                                     <Query>
                                     </Query>
                                 </View>";
 
 

After getting that, we can find, whether it is a File or Folder using the Content Type or the FileSystemObjectType.

The complete piece of code will be

 CamlQuery camlQuery = new CamlQuery();
             camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                                     <Query>
                                     </Query>
                                 </View>";
             camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
             ListItemCollection listItems = list.GetItems(camlQuery);
             clientContext.Load(listItems);
             clientContext.ExecuteQuery();
 
             foreach (var item in listItems)
             {
                 if (item.FileSystemObjectType == FileSystemObjectType.File)
                 {
                     // This is a File
                 }
                 else if (item.FileSystemObjectType == FileSystemObjectType.Folder)
                 {
                     // This is a  Folder
                 }
             }
 

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment