Share via

Microsoft REST API with pagination to get all files of SharePoint Site or One Drive.

Amit Singh Rawat 731 Reputation points
2023-03-29T15:17:50.6266667+00:00

Hi,

I want to fetch files of complete SharePoint site and also for One Drive. But to achieve this I have to loop through every list is SharePoint and every item in One drive.

Is there any API exposed by Microsoft which will return meta data of all files within SP Site and One Drive. If yes do they have option for pagination.

Thanks.

Microsoft Security | Microsoft Graph
0 comments No comments

Answer accepted by question author
  1. HarmeetSingh7172 4,831 Reputation points
    2023-03-29T22:14:37.7066667+00:00

    Hello Amit Rawat,

    Thanks for posting.

    There's no endpoint to get all items in a drive with every file from every subfolder at once.

    To get the item-ID of a file that is present in a multi-subfolder level using Graph API, you need to list children of a drive and traverse through the folder structure by using '/children' endpoint. This needs to be done recursively to reach the target folder (The item-ID needs to be replaced with lower-level folder (Item-ID)). The response will have id, name of items and other details. You can identify whether an item is a folder or a file by looking for "files" or "folder" property for that specific Item in response. Refer drive Items resource.

    To list children in the root of the current user's drive: This will list all the items (folders and files) along with the Item-IDs present in drive at root level.

    GET https://graph.microsoft.com/v1.0//me/drive/root/children
    
    GET https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root/children
    

    To list children of a DriveItem with a known path: This returns the list of items in the children collection of the target item. Refer this.

    GET /drives/{drive-id}/root:/{path-relative-to-root}:/children

    Similar posts:

    Hope this helps.

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


0 additional answers

Sort by: Most helpful

Your answer

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