How do i navigate into subfolders using MSGraphAPI

OldButYoungDev 61 Reputation points
2022-11-10T19:10:17.903+00:00

So i was finally able to list the contents of a folder that exists in a sharepoint drive in a manner that i wished using the below msgraph url

GET https://graph.microsoft.com/v1.0/sites/{guid}/drives/{guid}/root/children

The above http request lists the folders in the manner i wish to see them...

For example, i get an array such as :
{
id: "xxx",
"name": "Summer 2022",
"webUrl": "https://xxxx",
...
}
This array contains only the items within the root of the folder, which is exactly what i need.

However, i am building an web app where i want to create an html button/link that when clicked, it will list the contents of the folders.
Very similar to how 'file explorer" or "finder" in mac functions.

In finder/explorer, You double click folder, you see the contents.. if you click another folder inside.. you see the contents... and so on.

However, i am not sure how to do this using msgraph... i do not know how to "chain".

i wish to do something like this:

https://graph.microsoft.com/v1.0/sites/{guid}/drives/{parentFolder}/root/children
https://graph.microsoft.com/v1.0/sites/{guid}/drives/{parentFolder}/{PicturesFolder}/root/children
https://graph.microsoft.com/v1.0/sites/{guid}/drives/{parentFolder}/{PicturesFolder}/{Summer2022Folder}/root/children
https://graph.microsoft.com/v1.0/sites/{guid}/drives/{parentFolder}/{PicturesFolder}/{Summer2022Folder}/{BeachPhotosFolder}/root/children

hopefully you get the idea...

I just do not know what msgraph api url's to use.

I also need to create a "back" button that will go back up to the parent folder.. so when i click this html back button.. it will go one step back (very simliar to how file-explorer/finder does it)

any help would be great

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,414 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,803 questions
{count} votes

Accepted answer
  1. HarmeetSingh7172 4,811 Reputation points
    2022-11-10T20:12:26.153+00:00

    Hello @OldButYoungDev

    Thanks for reaching out!

    You can use https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/items/{folder-id}/children endpoint to list down the children of a folder in the drive. Please note that here folder-id is nothing but an item-id.

    In above endpoint, you need to pass parentfolderID of a folder for which you need to list down the children. Post that, you can replace the parentfolderID with the childFolderID, which will further list the children of childFolder. More precisely, you need to make multiple calls to get into the last child folder.

    Steps to follow:
    https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root/children - will list all the children of drive,

    followed by
    https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/items/{folder-id}/children OR
    https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/items/{item-id}

    Screenshot for reference:

    259211-screenshot1.png

    Hope I have answered your query.

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

    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Zehui Yao_MSFT 5,846 Reputation points
    2022-11-23T08:08:03.803+00:00

    Hi @OldButYoungDev , another option is that you can open the subfolders in the folder in turn through the method:

    https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root:/{FolderName}/{subfolder1Name}/{subfolder2Name}:/children  
    

    which like this:
    263364-image.png
    263336-image.png

    And you can get files in subfolders by:

    https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root:/{FolderName}/{subfolder1Name}/{subfolder2Name}/{fileName}  
    

    263289-image.png
    Hope this can help. Best Wishes.


    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.

  2. OldButYoungDev 61 Reputation points
    2022-12-02T20:01:13.26+00:00

    thank you so much guys! Helped out a lot.. wish i can give you both guys ACCEPT AS ANSWER. thank you!

    0 comments No comments