Microsoft Graph API : Is it possible to traverse recursive over Folders
Asked Answered
H

2

2

We are converting from mail EWS into Microsoft office 365 Graph API,

I want to flat all my Folder tree into list of flat folders, so each entry in list should contain:

Folder:{parentId, myId} , ...

I saw in beta version there is an option to traverse with :

https://graph.microsoft.com/beta/me/mailFolders/inbox?$top=50&$expand=childFolders($levels=5)

but unfortunately I get only the first Level...

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#users('4ed9f9bf-cfea-47de-ba2c-e4323d2dd600')/mailFolders/$entity",
    "id": "AQMkAGM5MDIzODk0LTg2NgBjLTQxN2EtOTNmMC0wYWY4M2VkMTg1MWEALgAAAxfTs4ItP_BBtfaokkhWqiMBAJ1RfNBJd7VOqHUq_F8aPdIAAAIBDQAAAA==",
    "displayName": "Inbox",
    "parentFolderId": "AQMkAGM5MDIzODk0LTg2NgBjLTQxN2EtOTNmMC0wYWY4M2VkMTg1MWEALgAAAxfTs4ItP_BBtfaokkhWqiMBAJ1RfNBJd7VOqHUq_F8aPdIAAAIBCQAAAA==",
    "childFolderCount": 1,
    "unreadItemCount": 307,
    "totalItemCount": 320,
    "wellKnownName": "inbox",
    "[email protected]": "https://graph.microsoft.com/beta/$metadata#users('4ed9f9bf-cfea-47de-ba2c-e4323d2dd600')/mailFolders('AQMkAGM5MDIzODk0LTg2NgBjLTQxN2EtOTNmMC0wYWY4M2VkMTg1MWEALgAAAxfTs4ItP_BBtfaokkhWqiMBAJ1RfNBJd7VOqHUq_F8aPdIAAAIBDQAAAA%3D%3D')/childFolders",
    "childFolders": [
        {
            "id": "AAMkAGM5MDIzODk0LTg2NmMtNDE3YS05M2YwLTBhZjgzZWQxODUxYQAuAAAAAAAX07OCLT-gQbX2qJJIVqojAQCdUXzQSXe1Tqh1KvhfGj3SAAOojCnvAAA=",
            "displayName": "LEVEL_1",
            "parentFolderId": "AQMkAGM5MDIzODk0LTg2NgBjLTQxN2EtOTNmMC0wYWY4M2VkMTg1MWEALgAAAxfTs4ItP_BBtfaokkhWqiMBAJ1RfNBJd7VOqHUq_F8aPdIAAAIBDQAAAA==",
            "childFolderCount": 1,
            "unreadItemCount": 0,
            "totalItemCount": 0,
            "wellKnownName": null
        }
    ]
}

Is there a good API either with O-Data or maybe other traversal algorithm (like in EWS - Deep traversal ) , so I can reduce my rest API calls ...

Thanks =]

Hexyl answered 14/2, 2018 at 14:58 Comment(0)
D
4

There isn't a deep folder traversal call in the Microsoft Graph API. You will need to expand each level. You may want to open a feature request for this.

With that said, you could use the batch feature to reduce the number of calls you have to make to get the folder hierarchy. You could have a single batch call to get all folders at a given level across hierarchies as long as the folder hierarchy isn't wider than 20 folders at any level (see batch restrictions). You'd want to account for more than 20 folders at any level.

With regards to an algorithm to do this, I haven't seen this scenario so you might be originator of the Microsoft Graph deep folder traversal with batching algorithm.

Dewclaw answered 14/2, 2018 at 18:47 Comment(4)
Thanks for your response. Is it still relevant nowadays ? Can I keep track if new folder was created by delta query on root folder, if folder was created on 2-3rd levels ?Thermoluminescent
@DmytroSh. Yes, deep folder traversal is still not a thing. If you mean the mailbox root folder, Graph doesn't give access to that folder. If you mean the next level default folders in message sub-tree, like inbox, sent item, drafts, etc, you can use DQ, but I doubt that it will discover deep changes. I hope it discovers changes changes to childFolderCount. I highly suggest you test it to see if that will work.Dewclaw
Links are deathWaddle
Links have been fixed. Links are life. @LucaZieglerDewclaw
V
2

I found this answer in a similar post and it is in my opinion the easiest way to retrieve all mail folders and subfolders recursiveley in a flat list.

Yes, you can. Simply use delta query to get all the folders.

Request Example: https://graph.microsoft.com/v1.0/users/[user_id]/mailfolders/delta?$select=displayname You get an array of all the folders, with child folders just after the parent-folder item in the response.

Test: Go to : https://developer.microsoft.com/en-us/graph/graph-explorer GET Version: v1.0 URL: https://graph.microsoft.com/v1.0/me/Mailfolders/delta Run Query

Notice "Internal Screens" and "Project Falcon" folders whose parentFolderId is the ID of "Inbox" are also included in the response.

Vinous answered 13/2, 2022 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.