How to list Files in Shared Documents

ArmDezii 41 Reputation points
2022-05-11T13:18:40.077+00:00

How can I programmatically list Files in a Folder under Shared Documents on a SharePoint Site? I have listed the URL below and I am looking to retrieve a listing of all *.jpgs in the Photo Folder. This would be analogous to recursively using the Dir() Function in VBA to achieve this end, however, this approach will not work in this context. Thanks in advance for any suggestions

https://organization.sharepoint.com/sites/FOHTeam2/Shared%20Documents/Photos/*.jpg
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,536 questions
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 30,666 Reputation points Microsoft Vendor
    2022-05-12T06:30:10.96+00:00

    Hi @ArmDezii ,
    Our forum mainly focus on C# code. I can provide you with a C# solution. As a workaround, you can try to use some tools to convert C# code to vba.

    ClientContext cxt = new ClientContext("http://xxx/sites/test");  
    List list = cxt.Web.Lists.GetByTitle("Documents");  
      
    cxt.Load(list);  
    cxt.Load(list.RootFolder);  
    cxt.Load(list.RootFolder.Folders);  
    cxt.Load(list.RootFolder.Files);  
    cxt.ExecuteQuery();  
    FolderCollection fcol = list.RootFolder.Folders;  
    List<string> lstFile = new List<string>();  
    foreach(Folder f in fcol)  
    {  
        if (f.Name.EndsWith(".jpg"))  
        {  
            cxt.Load(f.Files);  
            cxt.ExecuteQuery();  
            FileCollection fileCol = f.Files;  
            foreach (File file in fileCol)  
            {  
                lstFile.Add(file.Name);  
            }  
        }  
    }  
    

    Since your issue is about sharepoint development with vba. You can also get some more professional answer in techcommunity


    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 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. sadomovalex 3,626 Reputation points
    2022-05-11T14:21:56.943+00:00

    the following methods may be used both for Sharepoint on-prem and online:

    JSOM solution: How to retrieve files inside folder using javascript
    CSOM solution: How to get all the files inside the folder in list
    PowerShell solution: Get All Files from a Folder using PowerShell

    The following method will work only in on-prem:
    Server OM: How to read items from folder in SharePoint List/Library Programmatically C#