filesharepointcsomcaml

How to speed up file reading using SharePoint CSOM?


I have created a function using the SharePoint CSOM to extract all files within a Document Collection (List) using the below code however it takes over 5 minutes to get a list of 3671 files.

Is it possible to speed this up and if so How? I've seen other people use CAML Queries to extract files from a folder although I'm not 100% sure on how I would implement that

    static SharePointOnlineCredentials SPCreds = new SharePointOnlineCredentials("*********", new System.Net.NetworkCredential("", "*******").SecurePassword);
    static string sharepointURL = "https://********";
    static string ListName = "Documents";
    static string docuemntsStartWith = "Shared Documents";

    static List<string> files = new List<string>();

    static void Main(string[] args)
    {
        using (ClientContext clientContext = new ClientContext(sharepointURL))
        {
            clientContext.Credentials = SPCreds;
            List list = clientContext.Web.Lists.GetByTitle(ListName);                
            clientContext.Load(list);
            getFilesInFolder(clientContext, list.RootFolder);
        }
        Console.WriteLine("Extracted all " + files.Count + " files");
        Console.Read();
        foreach(string f in files)
        {
            Console.WriteLine(f);
        }
        Console.WriteLine("DONE");

        Console.Read();
    }

    public static void getFilesInFolder(ClientContext cc, SP.Folder SPFolder)
    {
        cc.Load(SPFolder);
        cc.Load(SPFolder.Folders);
        cc.Load(SPFolder.Files);
        cc.ExecuteQuery();
        FolderCollection fcol = SPFolder.Folders;
        FileCollection filecol = SPFolder.Files;
        foreach (SP.File SPF in filecol)
        {
            files.Add(SPF.ServerRelativeUrl);
        }

        foreach (SP.Folder SPF in fcol)
        {
            getFilesInFolder(cc, SPF);
        }
    }

Solution

  • Try below code this uses CAML query :

    using System;  
    using Microsoft.SharePoint.Client;  
    using SP = Microsoft.SharePoint.Client;  
      
    namespace Microsoft.SDK.SharePointServices.Samples  
    {  
        class RetrieveListItems  
        {  
            static void Main()  
            {  
                string siteUrl = "http://gauti.sharepoint.com/sites/SP";  
      
                ClientContext clientContext = new ClientContext(siteUrl);  
                SP.List oList = clientContext.Web.Lists.GetByTitle("NewBook");  
      
                CamlQuery camlQuery = new CamlQuery();  
                camlQuery.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +  
                    "<Value Type='Number'>19</Value></Geq></Where></Query><RowLimit>100</RowLimit></View>";  
      
      
                ListItemCollection collListItem = oList.GetItems(camlQuery);  
      
                clientContext.Load(collListItem);  
      
                clientContext.ExecuteQuery();  
      
                foreach (ListItem oListItem in collListItem)  
                {  
                    Console.WriteLine("ID: {0} \nTitle: {1} \nBody: {2}", oListItem.Id, oListItem["Title"], oListItem["Body"]);  
                }  
            }  
        }  
    }