How to recursively read all items using graph api for drive ?

Naresh Goty 20 Reputation points
2023-07-14T18:43:00.8766667+00:00

Hello,

I have requirement to read all the files metadata from the drive using graph api.

The composition of drive includes thousands of folder in multi-level hierarchy. I have implemented a BFS approach which reads each folder and all files from the provided top folder, and store the new folder names for subsequent reads for files.

But when I saw this video demo on pagination, it was mentioned that 'expand=children' can able to do the same without additional handling of navigating sub-folders. I tried using the API, the response does not contain nextLink on children even if child folder has more files. Is this functionality working as expected or did I miss something here?

Video demo: https://learn.microsoft.com/en-us/graph/paging

API I tried from video: https://graph.microsoft.com/v1.0/me/drives?$expand=root($expand=children)

My drive has folders in multiple levels:

root/folder1/folder2...../folderN

Current Approach (which is working):

This approach, I have to build the endpoint based on folder discovered, which seems bit complex than what is found from video demo. Any thoughts would be appreciated? Thanks!

https://graph.microsoft.com/v1.0/me/drive/root:/folderName/......:/children
Microsoft 365 and Office | OneDrive | For business | Windows
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Sourabh Gupta 800 Reputation points Microsoft External Staff
    2024-02-18T15:01:11.01+00:00

    Hi Naresh Goty, Thanks for reaching out. You can use the following code to recursively read all the items in a drive using graph api.

    private async Task<List<DriveItem>> GetAllFilesFromOneDriveSubFolderAsync(GraphServiceClient graphClient, DriveItem oneDriveSubFolder)
    {
        var subDriveItems = new List<DriveItem>();
        var subDriveItemFiles = new List<DriveItem>();
    
        //Fetching out all the files and sub folders inside the subfolder
        IDriveItemChildrenCollectionPage items = await graphClient.Drives[oneDriveSubFolder.ParentReference.DriveId]
                                                                    .Items[oneDriveSubFolder.Id].Children
                                                                    .Request()
                                                                    .GetAsync();
        subDriveItems.AddRange(items.CurrentPage);
    
        //Checking if there are more than one pages, if so fetch out from all the subsequent pages
        var next = items.NextPageRequest;
        while (next != null)
        {
            items = await next.GetAsync();
            subDriveItems.AddRange(items.CurrentPage);
            next = items.NextPageRequest;
        }
    
        //In case there is still sub folder then repeating the process to fetch out files
        foreach (DriveItem driveItem in subDriveItems)
        {
            if (driveItem.Folder != null)
            {
                subDriveItemFiles.AddRange(await GetAllFilesFromOneDriveSubFolderAsync(graphClient, driveItem));
            }
            else if (driveItem.File != null)
            {
                subDriveItemFiles.Add(driveItem);
            }
       }
        return subDriveItemFiles;
    }
    
    
            public async Task<List<DriveItem>> GetUsersOneDriveRootFilesAsync(GraphServiceClient graphClient, string userId, string driveId)
            {
                var driveItems = new List<DriveItem>();
                var driveItemFiles = new List<DriveItem>();
    
                //Fetching out all the files and sub folders inside the root folder of users one drive
                var items = await graphClient.Drives[driveId]
                                                .Root.ItemWithPath("/Templates")
                                                .Children
                                                .Request()
                                                .WithMaxRetry(3, 3)
                                                .GetAsync();
    
                driveItems.AddRange(items.CurrentPage);
    
                //Checking if there are more than one pages, if so fetch out from all the subsequent pages
                var next = items.NextPageRequest;
                while (next != null)
                {
                    items = await next.GetAsync();
                    driveItems.AddRange(items.CurrentPage);
                    next = items.NextPageRequest;
                }
    
                // In case there is still sub folder then fetching out the files from the folder.
                foreach (DriveItem driveItem in driveItems)
                {
                    if (driveItem.Folder != null)
                    {
                        driveItemFiles.AddRange(await GetAllFilesFromOneDriveSubFolderAsync(graphClient, driveItem));
                    }
                    else if (driveItem.File != null)
                    {
                        driveItemFiles.Add(driveItem);
                    }
                }
                return driveItemFiles;
            }
    
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  3. Ascanio Vandooren 0 Reputation points
    2024-08-22T05:08:49.76+00:00

    Unfortunatly this code is out of date, this is not working for me

    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.