PowerBuilder Requires Chilkat v9.5.0.67+
PowerBuilder
Outlook -- Create Map of Folder Paths to IDs
See more Outlook Examples
Recursively descends folders and creates a hashtable of folder paths to IDs. This can be done once at the start of your program (or even less if the map is persisted to a file or database).The reason this is necessary is because folder ID's need to be passed to the Outlook API, and an application will typically be working with folder paths.
This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_FolderMap
oleobject loo_SbResponse
oleobject loo_Json
oleobject loo_IdQueue
oleobject loo_SbFolderPath
oleobject loo_SbQueueEntry
string ls_FolderName
string ls_FolderPath
string ls_FolderId
long i
long li_NumFolders
string ls_ParentFolderPath
string ls_ParentFolderId
oleobject loo_SbFolderMapXml
oleobject loo_Ht2
oleobject loo_Sb2
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
// Our folder path --> ID map will be stored in this hash table.
loo_FolderMap = create oleobject
li_rc = loo_FolderMap.ConnectToNewObject("Chilkat.Hashtable")
// Use your previously obtained access token here:
// See the following examples for getting an access token:
// Get Microsoft Graph OAuth2 Access Token (Azure AD v2.0 Endpoint).
// Get Microsoft Graph OAuth2 Access Token (Azure AD Endpoint).
// Refresh Access Token (Azure AD v2.0 Endpoint).
// Refresh Access Token (Azure AD Endpoint).
loo_Http.AuthToken = "MICROSOFT_GRAPH_ACCESS_TOKEN"
loo_SbResponse = create oleobject
li_rc = loo_SbResponse.ConnectToNewObject("Chilkat.StringBuilder")
// Begin by getting the top-level folders.
loo_Http.ClearUrlVars()
loo_Http.SetUrlVar("userPrincipalName","chilkatsoft@outlook.com")
li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/users/{$userPrincipalName}/mailFolders",loo_SbResponse)
if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
Write-Debug loo_Http.LastErrorText
destroy loo_Http
destroy loo_FolderMap
destroy loo_SbResponse
return
end if
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.LoadSb(loo_SbResponse)
loo_Json.EmitCompact = 0
Write-Debug "Status code = " + string(loo_Http.LastStatus)
if loo_Http.LastStatus <> 200 then
Write-Debug loo_Json.Emit()
Write-Debug "Failed."
end if
// This is our queue/stack of unprocessed folder ID's
// The recursive nature of this example is that we get the
// child folders for each folder ID in the idQueue, which may
// cause additional ID's to be added. We continue until the idQueue
// is empty.
loo_IdQueue = create oleobject
li_rc = loo_IdQueue.ConnectToNewObject("Chilkat.StringArray")
loo_SbFolderPath = create oleobject
li_rc = loo_SbFolderPath.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbQueueEntry = create oleobject
li_rc = loo_SbQueueEntry.ConnectToNewObject("Chilkat.StringBuilder")
// Prime the map and idQueue with the top-level folders.
i = 0
li_NumFolders = loo_Json.SizeOfArray("value")
do while i < li_NumFolders
loo_Json.I = i
ls_FolderName = loo_Json.StringOf("value[i].displayName")
ls_FolderId = loo_Json.StringOf("value[i].id")
loo_SbFolderPath.SetString("/")
loo_SbFolderPath.Append(ls_FolderName)
ls_FolderPath = loo_SbFolderPath.GetAsString()
loo_FolderMap.AddStr(ls_FolderPath,ls_FolderId)
Write-Debug ls_FolderPath + " --> " + ls_FolderId
// Push the folder path + id onto the idQueue.
loo_SbQueueEntry.Clear()
loo_SbQueueEntry.SetNth(0,ls_FolderPath,"|",0,0)
loo_SbQueueEntry.SetNth(1,ls_FolderId,"|",0,0)
loo_IdQueue.Append(loo_SbQueueEntry.GetAsString())
i = i + 1
loop
// Initial output:
// /Archive --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAG8XunwAAAA=
// /Deleted Items --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEKAAAA
// /Drafts --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEPAAAA
// /Inbox --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA
// /Junk Email --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEiAAAA
// /Outbox --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgELAAAA
// /Sent Items --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEJAAAA
//
// Process the idQueue until it becomes empty. This is the recursive loop.
do while loo_IdQueue.Length > 0
loo_SbQueueEntry.SetString(loo_IdQueue.GetString(0))
loo_IdQueue.RemoveAt(0)
ls_ParentFolderPath = loo_SbQueueEntry.GetNth(0,"|",0,0)
ls_ParentFolderId = loo_SbQueueEntry.GetNth(1,"|",0,0)
loo_Http.SetUrlVar("id",ls_ParentFolderId)
li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/users/{$userPrincipalName}/mailFolders/{$id}/childFolders",loo_SbResponse)
if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
Write-Debug loo_Http.LastErrorText
destroy loo_Http
destroy loo_FolderMap
destroy loo_SbResponse
destroy loo_Json
destroy loo_IdQueue
destroy loo_SbFolderPath
destroy loo_SbQueueEntry
return
end if
loo_Json.LoadSb(loo_SbResponse)
if loo_Http.LastStatus <> 200 then
Write-Debug "Status code = " + string(loo_Http.LastStatus)
Write-Debug loo_Json.Emit()
Write-Debug "Failed."
end if
i = 0
li_NumFolders = loo_Json.SizeOfArray("value")
do while i < li_NumFolders
loo_Json.I = i
ls_FolderName = loo_Json.StringOf("value[i].displayName")
ls_FolderId = loo_Json.StringOf("value[i].id")
loo_SbFolderPath.SetString(ls_ParentFolderPath)
loo_SbFolderPath.Append("/")
loo_SbFolderPath.Append(ls_FolderName)
ls_FolderPath = loo_SbFolderPath.GetAsString()
loo_FolderMap.AddStr(ls_FolderPath,ls_FolderId)
Write-Debug ls_FolderPath + " --> " + ls_FolderId
// Push the folder path + id onto the idQueue.
loo_SbQueueEntry.Clear()
loo_SbQueueEntry.SetNth(0,ls_FolderPath,"|",0,0)
loo_SbQueueEntry.SetNth(1,ls_FolderId,"|",0,0)
loo_IdQueue.Append(loo_SbQueueEntry.GetAsString())
i = i + 1
loop
loop
// The hash table of mail folder paths --> ID's can be persisted to XML and saved to a file or database (or anywhere..)
loo_SbFolderMapXml = create oleobject
li_rc = loo_SbFolderMapXml.ConnectToNewObject("Chilkat.StringBuilder")
loo_FolderMap.ToXmlSb(loo_SbFolderMapXml)
loo_SbFolderMapXml.WriteFile("qa_data/outlook/folderMap.xml","utf-8",0)
// The hash table can be restored from the serialized XML like this:
loo_Ht2 = create oleobject
li_rc = loo_Ht2.ConnectToNewObject("Chilkat.Hashtable")
loo_Sb2 = create oleobject
li_rc = loo_Sb2.ConnectToNewObject("Chilkat.StringBuilder")
loo_Sb2.LoadFile("qa_data/outlook/folderMap.xml","utf-8")
loo_Ht2.AddFromXmlSb(loo_Sb2)
// What's the ID for the folder "/Inbox/abc/subFolderA" ?
Write-Debug "id for /Inbox/abc/subFolderA = " + loo_Ht2.LookupStr("/Inbox/abc/subFolderA")
// Final output:
// /Archive --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAG8XunwAAAA=
// /Deleted Items --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEKAAAA
// /Drafts --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEPAAAA
// /Inbox --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA
// /Junk Email --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEiAAAA
// /Outbox --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgELAAAA
// /Sent Items --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEJAAAA
// /Inbox/abc --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huv8AAAA=
// /Inbox/xyz --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwEAAAA=
// /Inbox/abc/subFolderA --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwAAAQ==
// /Inbox/abc/subFolderB --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwMAAAA=
// /Inbox/abc/subFolderA/a --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwIAAAA=
//
// ------------------------------------------------------------------------------------------------------
// This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com
//
// The Microsoft Graph Outlook Mail API lets you read, create, and send messages and attachments,
// view and respond to event messages, and manage folders that are secured by Azure Active Directory
// in Office 365. It also provides the same functionality in Microsoft accounts specifically
// in these domains: Hotmail.com, Live.com, MSN.com, Outlook.com, and Passport.com.
destroy loo_Http
destroy loo_FolderMap
destroy loo_SbResponse
destroy loo_Json
destroy loo_IdQueue
destroy loo_SbFolderPath
destroy loo_SbQueueEntry
destroy loo_SbFolderMapXml
destroy loo_Ht2
destroy loo_Sb2