Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

PowerBuilder Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(PowerBuilder) Outlook -- Search Multiple Folders

I don't yet see how it's possible to do a recursive search of Outlook folders using the Microsoft Graph API. My best guess is to somehow use OData v4.0's $expand query option, but the hierarchical structure of the "mailFolder" and "messages" Microsoft Graph resources don't quite fit. Suggestions are welcome and can be sent to support@chilkatsoft.com.

This example will iterate over a list of folder previously obtained by a recursive traversal of the Outlook mail folders. (See the link in the example code below.)

A separate search is performed on each desired folder, and the results are combined into a single result set.

Note: This example requires Chilkat v9.5.0.68 or greater.

This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
oleobject loo_Http
oleobject loo_XmlMap
integer li_Success
oleobject loo_Json
oleobject loo_JsonCombined
oleobject loo_SbResponse
oleobject loo_SbPath
integer li_NumFolders
integer i
integer j
integer k
integer li_NumMessages

// 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_9_5_0.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// 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"

// This example will iterate over the folders previously discovered by recursively traversing the Outlook folders
// as shown in this example:  Outlook Recursive Folder Traversal)

// The XML map produced by the recursive traversal looks like this:

// 	<?xml version="1.0" encoding="utf-8"?>
// 	<hashtable>
// 	<e><k>/Sent Items</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEJAAAA</v></e>
// 	<e><k>/Inbox</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA</v></e>
// 	<e><k>/Junk Email</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEiAAAA</v></e>
// 	<e><k>/Inbox/xyz</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwEAAAA=</v></e>
// 	<e><k>/Inbox/abc/subFolderA/a</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwIAAAA=</v></e>
// 	<e><k>/Inbox/abc</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huv8AAAA=</v></e>
// 	<e><k>/Outbox</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgELAAAA</v></e>
// 	<e><k>/Inbox/abc/subFolderA</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwAAAQ==</v></e>
// 	<e><k>/Inbox/abc/subFolderB</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwMAAAA=</v></e>
// 	<e><k>/Archive</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAG8XunwAAAA=</v></e>
// 	<e><k>/Deleted Items</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEKAAAA</v></e>
// 	<e><k>/Drafts</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEPAAAA</v></e>
// 	</hashtable>

// We'll iterate over the folders, and search all folders beginning with "/Inbox" (effectively, we're recursively searching
// everything under Inbox)
loo_XmlMap = create oleobject
li_rc = loo_XmlMap.ConnectToNewObject("Chilkat_9_5_0.Xml")

li_Success = loo_XmlMap.LoadXmlFile("qa_data/outlook/folderMap.xml")
if li_Success <> 1 then
    Write-Debug "Failed to load XML folder map."
    destroy loo_Http
    destroy loo_XmlMap
    return
end if

// We're going to return just the message id, subject, and FROM name/address.
loo_Http.SetUrlVar("select","id,subject,from")

// Our search will be for emails with the word "sample" in the subject.
loo_Http.SetUrlVar("filter","contains(subject,'sample')")

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_Json.EmitCompact = 0

loo_JsonCombined = create oleobject
li_rc = loo_JsonCombined.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_JsonCombined.EmitCompact = 0

loo_SbResponse = create oleobject
li_rc = loo_SbResponse.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

loo_SbPath = create oleobject
li_rc = loo_SbPath.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

li_NumFolders = loo_XmlMap.NumChildren
i = 0
j = 0
k = 0
do while i < li_NumFolders
    loo_XmlMap.I = i
    loo_SbPath.SetString(loo_XmlMap.GetChildContent("e[i]|k"))

    if loo_SbPath.StartsWith("/Inbox",1) = 1 then

        Write-Debug "------------------------------------------------------------------"
        Write-Debug "Searching " + loo_SbPath.GetAsString()

        // Search this mail folder..
        loo_Http.SetUrlVar("folder_id",loo_XmlMap.GetChildContent("e[i]|v"))

        loo_SbResponse.Clear()
        li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse)
        if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
            Write-Debug loo_Http.LastErrorText
            destroy loo_Http
            destroy loo_XmlMap
            destroy loo_Json
            destroy loo_JsonCombined
            destroy loo_SbResponse
            destroy loo_SbPath
            return
        end if

        loo_Json.LoadSb(loo_SbResponse)

        if loo_Http.LastStatus <> 200 then
            Write-Debug "HTTP response status = " + string(loo_Http.LastStatus)
            Write-Debug loo_Json.Emit()
            Write-Debug "Failed."
            destroy loo_Http
            destroy loo_XmlMap
            destroy loo_Json
            destroy loo_JsonCombined
            destroy loo_SbResponse
            destroy loo_SbPath
            return
        end if

        Write-Debug loo_Json.Emit()

        // Each mail folder search will return JSON with a value array, which is non-empty if any matching messages were found:

        // 			{
        // 			  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA')/messages(id,subject,from)",
        // 			  "value": [
        // 				Individual messages, if any, are listed here.
        // 			  ]
        // 			}

        li_NumMessages = loo_Json.SizeOfArray("value")
        j = 0
        do while j < li_NumMessages
            loo_Json.J = j

            // Add this message to the final result set.
            loo_JsonCombined.K = k
            loo_JsonCombined.UpdateString("value[k].folderPath",loo_SbPath.GetAsString())
            loo_JsonCombined.UpdateString("value[k].id",loo_Json.StringOf("value[j].id"))
            loo_JsonCombined.UpdateString("value[k].subject",loo_Json.StringOf("value[j].subject"))
            loo_JsonCombined.UpdateString("value[k].from.emailAddress.name",loo_Json.StringOf("value[j].from.emailAddress.name"))
            loo_JsonCombined.UpdateString("value[k].from.emailAddress.address",loo_Json.StringOf("value[j].from.emailAddress.address"))
            k = k + 1

            j = j + 1
        loop

    end if

    i = i + 1
loop

// Show the final combined JSON search result.
Write-Debug "------------------------------------------------------------------"
Write-Debug "Combined Search Results:"
Write-Debug loo_JsonCombined.Emit()

// Sample output for the above program:

// ------------------------------------------------------------------
// Searching /Inbox
// {
//   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA')/messages(id,subject,from)",
//   "value": [
//     {
//       "@odata.etag": "W/\"CQAAABYAAADn68XtMop0TpsYJGpfKXY9AADOpwfr\"",
//       "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA5_vF7TKKdE6bGCRqXyl2PQAAAM6Jj10AAAA=",
//       "subject": "A sample email with Amazon in the body",
//       "from": {
//         "emailAddress": {
//           "name": "Chilkat Software",
//           "address": "support@chilkatsoft.com"
//         }
//       }
//     }
//   ]
// }
// 
// ------------------------------------------------------------------
// Searching /Inbox/xyz
// {
//   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwEAAAA%3D')/messages(id,subject,from)",
//   "value": [
//   ]
// }
// 
// ------------------------------------------------------------------
// Searching /Inbox/abc/subFolderA/a
// {
//   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwIAAAA%3D')/messages(id,subject,from)",
//   "value": [
//     {
//       "@odata.etag": "W/\"CQAAABYAAADn68XtMop0TpsYJGpfKXY9AADOpzfb\"",
//       "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwIAAADn68XtMop0TpsYJGpfKXY9AAAAzombFQAAAA==",
//       "subject": "Sample email from admin@chilkat.io",
//       "from": {
//         "emailAddress": {
//           "name": "Chilkat Software",
//           "address": "admin@chilkat.io"
//         }
//       }
//     }
//   ]
// }
// 
// ------------------------------------------------------------------
// Searching /Inbox/abc
// {
//   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huv8AAAA%3D')/messages(id,subject,from)",
//   "value": [
//   ]
// }
// 
// ------------------------------------------------------------------
// Searching /Inbox/abc/subFolderA
// {
//   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwAAAQ%3D%3D')/messages(id,subject,from)",
//   "value": [
//   ]
// }
// 
// ------------------------------------------------------------------
// Searching /Inbox/abc/subFolderB
// {
//   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwMAAAA%3D')/messages(id,subject,from)",
//   "value": [
//   ]
// }
// 
// ------------------------------------------------------------------
// Combined Search Results:
// {
//   "value": [
//     {
//       "folderPath": "/Inbox",
//       "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA5_vF7TKKdE6bGCRqXyl2PQAAAM6Jj10AAAA=",
//       "subject": "A sample email with Amazon in the body",
//       "from": {
//         "emailAddress": {
//           "name": "Chilkat Software",
//           "address": "support@chilkatsoft.com"
//         }
//       }
//     },
//     {
//       "folderPath": "/Inbox/abc/subFolderA/a",
//       "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwIAAADn68XtMop0TpsYJGpfKXY9AAAAzombFQAAAA==",
//       "subject": "Sample email from admin@chilkat.io",
//       "from": {
//         "emailAddress": {
//           "name": "Chilkat Software",
//           "address": "admin@chilkat.io"
//         }
//       }
//     }
//   ]
// }
// 


destroy loo_Http
destroy loo_XmlMap
destroy loo_Json
destroy loo_JsonCombined
destroy loo_SbResponse
destroy loo_SbPath

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.