Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.6.0+

Outlook Mail: Create a Folder Path-to-ID Map

See more Outlook Examples

Traverse the root and each child-folder collection, following pagination at every level, then save a Chilkat Hashtable as XML. Queue entries keep URLs and parent paths separately. Literal percent signs and slashes in display names become %25 and %2F in map keys; this is a local path convention, not a Graph URL. Duplicate keys stop the example instead of silently overwriting an ID.

The map is an account-specific snapshot. Use a private filename per mailbox, rebuild after rename/move/create/delete, and use atomic replacement in production. A traversal is not transactionally isolated from concurrent mailbox changes. The example stops at 1000 requests and never saves an incomplete traversal. For standard folders prefer the locale-independent well-known names; the displayed map paths use the actual localized names.

Delegated permission: Mail.ReadBasic.

Uses direct Chilkat.Http calls to Microsoft Graph v1.0 for an Outlook.com or Microsoft 365 online mailbox. Save a current Graph access token as access_token in the private file qa_data/tokens/microsoftGraph.json. Obtain it through the Microsoft identity platform authorization-code flow with PKCE for a public interactive client; request offline_access if a refresh token is needed. Consent to the mail permission stated here. Tokens for SMTP/IMAP/POP, Calendar-only, or Contacts-only access are insufficient.

/me requires a signed-in user and delegated permissions. An unattended Microsoft 365 application instead uses administrator-approved application permissions and /users/{id-or-UPN}; limit it to the intended mailboxes. Client credentials is not available for personal Outlook.com mailboxes. These requests access cloud mailbox data, not local PST files, and do not require Outlook to be running. National clouds require their own authority and Graph hosts.

The code checks the HTTP status separately from transport success and validates JSON before reading it. For 401, refresh or authorize again; for 403, check consent, mailbox access, and policy; for 429, honor Retry-After with a bounded retry. A failed or timed-out write can have an uncertain outcome: reconcile before repeating it. Keep tokens, message content, continuation URLs, and detailed diagnostics private.

Microsoft documentation

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
Local $iReplaceCount = 0
;  Supply a current Microsoft Graph token with this example's mail permissions.
;  Keep the token cache private and refresh expired tokens before running the example.
$oToken = ObjCreate("Chilkat.JsonObject")
$oToken.Strict = True
$bSuccess = $oToken.LoadFile("qa_data/tokens/microsoftGraph.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read the private Graph token cache." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("Expected a nonempty access_token in the JSON." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
$oHttp.ConnectTimeout = 20
$oHttp.ReadTimeout = 60
$oHttp.SetRequestHeader "Accept","application/json"
$oResp = ObjCreate("Chilkat.HttpResponse")
$oResult = ObjCreate("Chilkat.JsonObject")
$oResult.Strict = True
;  Queue each collection URL together with its parent path; no delimiter-packed records.
$oUrls = ObjCreate("Chilkat.StringTable")
$oPaths = ObjCreate("Chilkat.StringTable")
$oUrls.Append("https://graph.microsoft.com/v1.0/me/mailFolders?includeHiddenFolders=true&$select=id,displayName,childFolderCount&$top=50")
$oPaths.Append("")
$oSeenLinks = ObjCreate("Chilkat.Hashtable")
$oNextLink = ObjCreate("Chilkat.StringBuilder")
$oSegment = ObjCreate("Chilkat.StringBuilder")
$oFullPath = ObjCreate("Chilkat.StringBuilder")
$oChildUrl = ObjCreate("Chilkat.StringBuilder")
$oFolderMap = ObjCreate("Chilkat.Hashtable")
Local $iQueueIndex = 0
While (($iQueueIndex < $oUrls.Count) And ($iQueueIndex < 1000))
Local $sUrl = $oUrls.StringAt($iQueueIndex)
Local $sParentPath = $oPaths.StringAt($iQueueIndex)
    $oNextLink.SetString($sUrl)
    If ($oNextLink.StartsWith("https://graph.microsoft.com/v1.0/",True) <> True) Then
        ConsoleWrite("Unexpected pagination host or API version; no request sent." & @CRLF)
        Exit
    EndIf

    If ($oSeenLinks.Contains($sUrl) = True) Then
        ConsoleWrite("Repeated pagination URL; stopping." & @CRLF)
        Exit
    EndIf

    $oSeenLinks.AddInt($sUrl,1)
    $bSuccess = $oHttp.HttpNoBody("GET",$sUrl,$oResp)
    ;  A false result can still include an HTTP error response.
    If (($bSuccess <> True) And ($oResp.StatusCode = 0)) Then
        ConsoleWrite("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying." & @CRLF)
        Exit
    EndIf

    If ($oResp.StatusCode <> 200) Then
        ConsoleWrite("Unexpected HTTP status: " & $oResp.StatusCode & @CRLF)
        ConsoleWrite("Request ID: " & $oResp.GetHeaderField("request-id") & @CRLF)
        If ($oResp.StatusCode = 429) Then
            ConsoleWrite("Retry-After: " & $oResp.GetHeaderField("Retry-After") & @CRLF)
        EndIf

        If ($oResp.StatusCode = 401) Then
            ConsoleWrite("Refresh the Graph access token or authorize again." & @CRLF)
        EndIf

        Exit
    EndIf

    $bSuccess = $oResult.Load($oResp.BodyStr)
    If ($bSuccess <> True) Then
        ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
        Exit
    EndIf

    If ($oResult.JsonTypeOf("value") <> 4) Then
        ConsoleWrite("Expected a value array." & @CRLF)
        Exit
    EndIf

Local $i = 0
    While ($i < $oResult.SizeOfArray("value"))
        $oResult.I = $i
        If (($oResult.JsonTypeOf("value[i].id") <> 1) Or ($oResult.StringOfEquals("value[i].id","",True) = True)) Then
            ConsoleWrite("Expected a nonempty value[i].id in the JSON." & @CRLF)
            Exit
        EndIf

        If (($oResult.JsonTypeOf("value[i].displayName") <> 1) Or ($oResult.StringOfEquals("value[i].displayName","",True) = True)) Then
            ConsoleWrite("Expected a nonempty value[i].displayName in the JSON." & @CRLF)
            Exit
        EndIf

Local $sFolderId = $oResult.StringOf("value[i].id")
        $oSegment.SetString($oResult.StringOf("value[i].displayName"))
        ;  Escape separators in display names to make the human-readable path unambiguous.
        $iReplaceCount = $oSegment.Replace("%","%25")
        $iReplaceCount = $oSegment.Replace("/","%2F")
        $oFullPath.SetString($sParentPath)
        $oFullPath.Append("/")
        $oFullPath.Append($oSegment.GetAsString())
Local $sFolderPath = $oFullPath.GetAsString()
        If ($oFolderMap.Contains($sFolderPath) = True) Then
            ConsoleWrite("Duplicate folder path; cannot safely overwrite the map." & @CRLF)
            Exit
        EndIf

        $bSuccess = $oFolderMap.AddStr($sFolderPath,$sFolderId)
        If ($bSuccess <> True) Then
            ConsoleWrite("Could not add the folder to the map." & @CRLF)
            Exit
        EndIf

        ConsoleWrite($sFolderPath & " -> " & $sFolderId & @CRLF)
        If ($oResult.IntOf("value[i].childFolderCount") > 0) Then
            $oChildUrl.SetString("https://graph.microsoft.com/v1.0/me/mailFolders/")
            $oChildUrl.Append($oHttp.UrlEncode($sFolderId))
            $oChildUrl.Append("/childFolders?includeHiddenFolders=true&$select=id,displayName,childFolderCount&$top=50")
            $oUrls.Append($oChildUrl.GetAsString())
            $oPaths.Append($sFolderPath)
        EndIf

        $i = $i + 1
    Wend
    ;  A quoted JSON path treats the period in @odata.nextLink literally.
Local $iNextType = $oResult.JsonTypeOf("""@odata.nextLink""")
    If (($iNextType = -1) Or ($iNextType = 6)) Then
        $sUrl = ""
    Else
        If (($iNextType <> 1) Or ($oResult.StringOfEquals("""@odata.nextLink""","",True) = True)) Then
            ConsoleWrite("Invalid continuation link; cannot establish completeness." & @CRLF)
            Exit
        EndIf

        $sUrl = $oResult.StringOf("""@odata.nextLink""")
    EndIf

    If ($sUrl <> "") Then
        $oUrls.Append($sUrl)
        $oPaths.Append($sParentPath)
    EndIf

    $iQueueIndex = $iQueueIndex + 1
Wend
If ($iQueueIndex < $oUrls.Count) Then
    ConsoleWrite("Stopped at the 1000-request demonstration limit; no partial map saved." & @CRLF)
    Exit
EndIf

$oFac = ObjCreate("Chilkat.FileAccess")
$bSuccess = $oFac.DirEnsureExists("qa_data/outlook")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not create the private output directory." & @CRLF)
    Exit
EndIf

$oMapXml = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oFolderMap.ToXmlSb($oMapXml)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not serialize the folder map." & @CRLF)
    Exit
EndIf

$bSuccess = $oMapXml.WriteFile("qa_data/outlook/folderMap.xml","utf-8",False)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not save the folder map." & @CRLF)
    Exit
EndIf

ConsoleWrite("Folder map saved after completing the traversal." & @CRLF)