How to get itemid of a file to be downloaded from Sharepoint folder which is under sub-sub folders using Graph APIs

Patil, Vinod V 40 Reputation points
2023-03-17T19:21:26.85+00:00

We can download a given file if we know the itemid of the file to be downloaded using below

https://graph.microsoft.com/v1.0/sites/{{siteid}}/drive/items/{{itemid}}/content

But how could we get the itemid of a file (test123.csv) which is present under sub-sub folder level

e.g. Folder A -> Subfolder B -> Subfolder C -> SUbfolder D -> test123.csv

We can display root -> Children and get the id of next Subfolder level but how can we get at even lower levels and that too with Graph API (not with other REST APIs)?

Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. HarmeetSingh7172 4,826 Reputation points
    2023-03-17T22:45:48.6133333+00:00

    Hello Patil, Vinod V

    Thanks for reaching out!

    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 /me/drive/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

    subfolder

    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.


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.