Get all documents from all subfolders in the document library - CSOM

I use the C # approach for the client side object model to get all list items from a document library containing subfolders. I checked the MSDN documentation and I was fixated on why I cannot get the field property, or if I even do it right.

NetworkCredential credentials = System.Net.CredentialCache.DefaultNetworkCredentials; ClientContext clientcontext = new ClientContext(Resources.defaultSPSite); clientcontext.Credentials = credentials; //Load Libraries from SharePoint //Web site = clientcontext.Web; clientcontext.Load(clientcontext.Web.Lists); clientcontext.ExecuteQuery(); //List sharedDocumentsList = clientcontext.Web.Lists.GetByTitle("TestLDOCS"); //CamlQuery camlQuery = new CamlQuery(); //camlQuery.ViewXml = @"<View Scope='Recursive'><Query></Query></View>"; foreach (List list in clientcontext.Web.Lists) { clientcontext.Load(list); clientcontext.ExecuteQuery(); //list.TemplateFeatureId.ToString().Equals("") && string baseType = list.BaseType.ToString(); string listTitle = list.Title.ToString(); if (list.BaseType.ToString().Equals("DocumentLibrary", StringComparison.InvariantCultureIgnoreCase) && list.Title.ToString().Equals("TestLDOCS", StringComparison.InvariantCultureIgnoreCase)) { foreach (Folder subFolder in list.RootFolder.Folders) { foreach (File f in subFolder.Files) { Console.WriteLine((string) f.Title); } } } } } 

The error I get is that collecting "foreach (File f in subFolder.Files)" cannot be an initialized error. In any case, to get the field values ​​of all documents in each subfolder in the document library using CSOM?

I know that you can strictly enter field values ​​also with the list element ie (listItem ["fieldName"]). Should I go along this route?

+6
source share
2 answers

Some recommendations:

1) Prefer the ClientRuntimeContext.LoadQuery method for loading specific lists, for example:

 var lists = ctx.LoadQuery(ctx.Web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary)); ctx.ExecuteQuery(); 

2) Since SharePoint SCOM supports Request Batching , it is recommended that you minimize the number of requests to the server. The following example shows how to execute a single request to a server to download all files from document libraries:

 foreach (var list in lists) { var items = list.GetItems(CreateAllFilesQuery()); ctx.Load(items, icol => icol.Include(i => i.File)); results[list.Title] = items.Select( i=>i.File); } ctx.ExecuteQuery(); 

3) Prefer to download all files using a CAML request, as shown below:

 public static CamlQuery CreateAllFilesQuery() { var qry = new CamlQuery(); qry.ViewXml ="<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Integer\">0</Value></Eq></Where></Query></View>"; return qry; } 

Then, in the following example, all files in the library will be returned:

 var items = list.GetItems(CreateAllFilesQuery()); ctx.Load(items, icol => icol.Include(i => i.File)); ctx.ExecuteQuery(); var files = items.Select( i=>i.File).ToList(); 

This is a more streamlined way to load specific lists from a performance perspective.

Full example

How to download all files from document libraries using SharePoint CSOM:

 using (var ctx = new ClientContext(webUri)) { var results = new Dictionary<string, IEnumerable<File>>(); var lists = ctx.LoadQuery(ctx.Web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary)); ctx.ExecuteQuery(); foreach (var list in lists) { var items = list.GetItems(CreateAllFilesQuery()); ctx.Load(items, icol => icol.Include(i => i.File)); results[list.Title] = items.Select( i=>i.File); } ctx.ExecuteQuery(); //Print results foreach (var result in results) { Console.WriteLine("List: {0}",result.Key); foreach (var file in result.Value) { Console.WriteLine("File: {0}", file.Name); } } } 
+7
source
 foreach (List list in clientcontext.Web.Lists) { clientcontext.Load(list); clientcontext.ExecuteQuery(); //list.TemplateFeatureId.ToString().Equals("") && string baseType = list.BaseType.ToString(); string listTitle = list.Title.ToString(); if (list.BaseType.ToString().Equals("DocumentLibrary", StringComparison.InvariantCultureIgnoreCase) && list.Title.ToString().Equals("TestLDOCS", StringComparison.InvariantCultureIgnoreCase)) { foreach (Folder subFolder in list.RootFolder.Folders) { clientcontext.Load(subFolder.Files); clientcontext.ExecuteQuery(); foreach (File f in subFolder.Files) { Console.WriteLine((string) f.Title); } } } } } 
+1
source

Source: https://habr.com/ru/post/944560/


All Articles