CSOM - Get files in folders

MW 1 Reputation point
2022-12-09T19:17:00.46+00:00

Can someone tell me why the following code doesn't work for items not at the root level? Something about listItem.File not being loaded (line 31)

Line 32 will fail with the following error:

  • The property or field 'Exists' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

Line 33 also fails

I don't know how to "request" the file...

      ListItemCollection collListItem = null;  
      ClientContext clientContext = null;  

            clientContext = new ClientContext(pageUri);  
            var site = clientContext.Site;  
            var web = clientContext.Web;  
            clientContext.Credentials = CredentialCache.DefaultCredentials;  

            clientContext.Load(web, w => w.Webs, w => w.Url);  
            clientContext.Load(web.Lists, lists => lists.Include(list => list.Title, list => list.Id, list => list.RootFolder)  
                                                        .Where(l => l.RootFolder.Name == listName));   
            clientContext.ExecuteQuery();  

            SP.List mylist = web.Lists.Single();   

            CamlQuery camlQuery = new CamlQuery();  
            camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"></View>";  
            collListItem = mylist.GetItems(camlQuery);  

            clientContext.Load(collListItem,  
                    items => items.Include(  
                    item => item.FieldValuesAsText,  
                    item => item.Id,  
                    item => item));  

            clientContext.ExecuteQuery();  

             
            foreach (ListItem listItem in collListItem)  
            {  
                listItem.File.CheckOut();  
                bool exists = listItem.File.Exists;  
                **clientContext.ExecuteQuery();**  
            }  
Microsoft 365 and Office SharePoint Development
{count} votes

3 answers

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-12-12T06:24:57.937+00:00

    Hi @MW ,

    According to my research and testing, I recommend you use the following code to get files in folder via CSOM:

            static void Main(string[] args)  
            {  
                var cc = GetonlineContext();  
                cc.Load(cc.Web, p => p.Title);  
                cc.ExecuteQuery();  
      
                List list = cc.Web.Lists.GetByTitle("<List-Name>");  
      
                cc.Load(list);  
                cc.Load(list.RootFolder);  
                cc.Load(list.RootFolder.Folders);  
                cc.Load(list.RootFolder.Files);  
                cc.ExecuteQuery();  
                FolderCollection fcol = list.RootFolder.Folders;  
                foreach (Folder f in fcol)  
                {  
                    if (f.Name == "<Folder-Name>")  
                    {  
                        cc.Load(f.Files);  
                        cc.ExecuteQuery();  
                        FileCollection fileCol = f.Files;  
                        foreach (File file in fileCol)  
                        {  
                            Debug.WriteLine(file.Name);  
                        }  
                    }  
                }  
      
            }  
    

    My test result:
    269592-01.png

    Hope it can help you. Thanks for your understanding.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. MW 1 Reputation point
    2022-12-15T18:55:54.633+00:00

    Turns out you get the error when the ListItem is a folder. ListItem.FileSystemObjectType is helpful to check.

    0 comments No comments

  3. Tong Zhang_MSFT 9,251 Reputation points
    2022-12-16T02:46:51.117+00:00

    Hi @MW ,

    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [CSOM - Get files in folders]

    Issue Symptom:

    Why the following code doesn't work for items not at the root level? Something about listItem.File not being loaded (line 31)

    Line 32 will fail with the following error:

    The property or field 'Exists' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.  
    

    Line 33 also fails

           ListItemCollection collListItem = null;  
           ClientContext clientContext = null;  
                 clientContext = new ClientContext(pageUri);  
                 var site = clientContext.Site;  
                 var web = clientContext.Web;  
                 clientContext.Credentials = CredentialCache.DefaultCredentials;  
                 clientContext.Load(web, w => w.Webs, w => w.Url);  
                 clientContext.Load(web.Lists, lists => lists.Include(list => list.Title, list => list.Id, list => list.RootFolder)  
                                                             .Where(l => l.RootFolder.Name == listName));   
                 clientContext.ExecuteQuery();  
                 SP.List mylist = web.Lists.Single();   
                 CamlQuery camlQuery = new CamlQuery();  
                 camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"></View>";  
                 collListItem = mylist.GetItems(camlQuery);  
                 clientContext.Load(collListItem,  
                         items => items.Include(  
                         item => item.FieldValuesAsText,  
                         item => item.Id,  
                         item => item));  
                 clientContext.ExecuteQuery();  
                     
                 foreach (ListItem listItem in collListItem)  
                 {  
                     listItem.File.CheckOut();  
                     bool exists = listItem.File.Exists;  
                     **clientContext.ExecuteQuery();**  
                 }  
    

    Solutions:

    Turns out you get the error when the ListItem is a folder. ListItem.FileSystemObjectType is helpful to check.

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread.

    Thanks for your understanding and support!


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.