Get All SharePoint Files and Folder in a Tree structure of a Document Library using GraphAPI?

Akshith Reddy Thalla 100 Reputation points
2024-05-06T14:51:31.3533333+00:00

Get All SharePoint Files and Folder in a Tree structure of a Document Library?

I can get all the files within the Document Library, but not in a structured way or in a tree format, by using - https://graph.microsoft.com/v1.0/drives/[driveId]/root/search(q='')

User's image

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,802 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,820 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 49,076 Reputation points
    2024-05-06T15:04:31.3666667+00:00

    You're using the search endpoint to get all the items that match the criteria (everything in your case). If you want to enumerate the drive one level at a time then you'll need to use the children endpoint. The first example given returns the children of the root. For each child you can then call its list children endpoint to get its children (if it is a folder). In this way you can emulate how Windows Explorer works by retrieving the items only when needed.

    Of course if you want all children at once then search is fine as making calls for every folder is expensive. Only load each folder on demand if you need that functionality. Also be aware that if you want to show an expand option for a folder but only if it has children then you'd technically need to load the root children and then the children of each root folder. Each time a child is expanded you would need to expand its child folders so you know whether it has any children to "expand". But that is only if you want to get fancy.