0

I'm currently using PNP Core Framework with a "normal" console application.

I want to get all Files and Folders from a specific Document Library. How can I do this in a simple way? I tried it with a CAML Query approach, but no real luck.

I'm currently stuck at this question.

Best regards

Matthias

1 Answer 1

0

see the following code

private static void UseRecursiveMethodToGetAllItems()
{
    using (var context = new ClientContext(WebUrl))
   {
        context.Credentials = new SharePointOnlineCredentials(UserName, Password);

        var rootFolders = context.Web.GetFolderByServerRelativeUrl(LibName).Folders;
        context.Load(rootFolders, folders => folders.Include(f => f.ListItemAllFields));
        context.ExecuteQuery();
        foreach (var folder in rootFolders)
        {
            GetFilesAndFolders(context, folder);
        }

        Console.ReadLine();
    }
}

private static void GetFilesAndFolders(ClientContext context, Folder folder)
{
    if (folder != null && folder.ListItemAllFields.FieldValues.Count > 0)
    {
        Console.WriteLine($"Folder - {folder.ListItemAllFields.FieldValues["FileLeafRef"]}");

        var fileCollection = folder.Files;
        context.Load(fileCollection, files => files.Include(f => f.ListItemAllFields));
        context.ExecuteQuery();

        foreach(var file in fileCollection)
        {
            Console.WriteLine($" -> {file.ListItemAllFields.FieldValues["FileLeafRef"]}");
        }

        var subFolderCollection = folder.Folders;
        context.Load(subFolderCollection, folders => folders.Include(f => f.ListItemAllFields));
        context.ExecuteQuery();
        foreach (var subFolder in subFolderCollection)
        {
            GetFilesAndFolders(context, subFolder);
        }
    }
}

enter image description here

Source : https://stackoverflow.com/questions/61323146/c-sharp-sharepoint-loop-through-all-files-in-folder-and-all-subfolders

2
  • Is this also with context.web.lists.GetBytitle("ListName") possible?
    – Matthias
    Mar 19, 2023 at 9:35
  • 1
    Hi, any updates?
    – Carl Zhao
    Apr 6, 2023 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.