Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
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
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) Outlook -- Create Map of Folder Paths to IDs

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.

Note: This example requires Chilkat v9.5.0.67 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

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Our folder path --> ID map will be stored in this hash table.
    DECLARE @folderMap int
    -- Use "Chilkat_9_5_0.Hashtable" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Hashtable', @folderMap OUT

    -- 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).

    EXEC sp_OASetProperty @http, 'AuthToken', 'MICROSOFT_GRAPH_ACCESS_TOKEN'

    DECLARE @sbResponse int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponse OUT

    -- Begin by getting the top-level folders.
    EXEC sp_OAMethod @http, 'ClearUrlVars', NULL
    DECLARE @success int
    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'userPrincipalName', 'chilkatsoft@outlook.com'
    DECLARE @success int
    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://graph.microsoft.com/v1.0/users/{$userPrincipalName}/mailFolders', @sbResponse
    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    IF (@success <> 1) and (@iTmp0 = 0)
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @folderMap
        EXEC @hr = sp_OADestroy @sbResponse
        RETURN
      END

    DECLARE @json int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'LoadSb', @success OUT, @sbResponse
    EXEC sp_OASetProperty @json, 'EmitCompact', 0


    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    PRINT 'Status code = ' + @iTmp0
    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN
        EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
      END

    -- 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.
    DECLARE @idQueue int
    -- Use "Chilkat_9_5_0.StringArray" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringArray', @idQueue OUT

    DECLARE @sbFolderPath int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbFolderPath OUT

    DECLARE @sbQueueEntry int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbQueueEntry OUT

    DECLARE @folderName nvarchar(4000)

    DECLARE @folderPath nvarchar(4000)

    DECLARE @folderId nvarchar(4000)

    -- Prime the map and idQueue with the top-level folders.
    DECLARE @i int
    SELECT @i = 0
    DECLARE @numFolders int
    EXEC sp_OAMethod @json, 'SizeOfArray', @numFolders OUT, 'value'
    WHILE @i < @numFolders
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @folderName OUT, 'value[i].displayName'
        EXEC sp_OAMethod @json, 'StringOf', @folderId OUT, 'value[i].id'
        EXEC sp_OAMethod @sbFolderPath, 'SetString', @success OUT, '/'
        EXEC sp_OAMethod @sbFolderPath, 'Append', @success OUT, @folderName
        EXEC sp_OAMethod @sbFolderPath, 'GetAsString', @folderPath OUT
        EXEC sp_OAMethod @folderMap, 'AddStr', @success OUT, @folderPath, @folderId


        PRINT @folderPath + ' --> ' + @folderId

        -- Push the folder path + id onto the idQueue.
        EXEC sp_OAMethod @sbQueueEntry, 'Clear', NULL
        EXEC sp_OAMethod @sbQueueEntry, 'SetNth', @success OUT, 0, @folderPath, '|', 0, 0
        EXEC sp_OAMethod @sbQueueEntry, 'SetNth', @success OUT, 1, @folderId, '|', 0, 0
        EXEC sp_OAMethod @sbQueueEntry, 'GetAsString', @sTmp0 OUT
        EXEC sp_OAMethod @idQueue, 'Append', @success OUT, @sTmp0
        SELECT @i = @i + 1
      END

    -- 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.
    DECLARE @parentFolderPath nvarchar(4000)

    DECLARE @parentFolderId nvarchar(4000)

    EXEC sp_OAGetProperty @idQueue, 'Length', @iTmp0 OUT
    WHILE @iTmp0 > 0
      BEGIN
        EXEC sp_OAMethod @idQueue, 'GetString', @sTmp0 OUT, 0
        EXEC sp_OAMethod @sbQueueEntry, 'SetString', @success OUT, @sTmp0
        EXEC sp_OAMethod @idQueue, 'RemoveAt', @success OUT, 0

        EXEC sp_OAMethod @sbQueueEntry, 'GetNth', @parentFolderPath OUT, 0, '|', 0, 0
        EXEC sp_OAMethod @sbQueueEntry, 'GetNth', @parentFolderId OUT, 1, '|', 0, 0

        EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'id', @parentFolderId
        EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://graph.microsoft.com/v1.0/users/{$userPrincipalName}/mailFolders/{$id}/childFolders', @sbResponse
        EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
        IF (@success <> 1) and (@iTmp0 = 0)
          BEGIN
            EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @folderMap
            EXEC @hr = sp_OADestroy @sbResponse
            EXEC @hr = sp_OADestroy @json
            EXEC @hr = sp_OADestroy @idQueue
            EXEC @hr = sp_OADestroy @sbFolderPath
            EXEC @hr = sp_OADestroy @sbQueueEntry
            RETURN
          END

        EXEC sp_OAMethod @json, 'LoadSb', @success OUT, @sbResponse
        EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
        IF @iTmp0 <> 200
          BEGIN

            EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
            PRINT 'Status code = ' + @iTmp0
            EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
            PRINT @sTmp0

            PRINT 'Failed.'
          END

        SELECT @i = 0
        EXEC sp_OAMethod @json, 'SizeOfArray', @numFolders OUT, 'value'
        WHILE @i < @numFolders
          BEGIN
            EXEC sp_OASetProperty @json, 'I', @i
            EXEC sp_OAMethod @json, 'StringOf', @folderName OUT, 'value[i].displayName'
            EXEC sp_OAMethod @json, 'StringOf', @folderId OUT, 'value[i].id'
            EXEC sp_OAMethod @sbFolderPath, 'SetString', @success OUT, @parentFolderPath
            EXEC sp_OAMethod @sbFolderPath, 'Append', @success OUT, '/'
            EXEC sp_OAMethod @sbFolderPath, 'Append', @success OUT, @folderName
            EXEC sp_OAMethod @sbFolderPath, 'GetAsString', @folderPath OUT
            EXEC sp_OAMethod @folderMap, 'AddStr', @success OUT, @folderPath, @folderId


            PRINT @folderPath + ' --> ' + @folderId

            -- Push the folder path + id onto the idQueue.
            EXEC sp_OAMethod @sbQueueEntry, 'Clear', NULL
            EXEC sp_OAMethod @sbQueueEntry, 'SetNth', @success OUT, 0, @folderPath, '|', 0, 0
            EXEC sp_OAMethod @sbQueueEntry, 'SetNth', @success OUT, 1, @folderId, '|', 0, 0
            EXEC sp_OAMethod @sbQueueEntry, 'GetAsString', @sTmp0 OUT
            EXEC sp_OAMethod @idQueue, 'Append', @success OUT, @sTmp0
            SELECT @i = @i + 1
          END

      END

    -- The hash table of mail folder paths --> ID's can be persisted to XML and saved to a file or database (or anywhere..)
    DECLARE @sbFolderMapXml int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbFolderMapXml OUT

    EXEC sp_OAMethod @folderMap, 'ToXmlSb', @success OUT, @sbFolderMapXml
    EXEC sp_OAMethod @sbFolderMapXml, 'WriteFile', @success OUT, 'qa_data/outlook/folderMap.xml', 'utf-8', 0

    -- The hash table can be restored from the serialized XML like this:
    DECLARE @ht2 int
    -- Use "Chilkat_9_5_0.Hashtable" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Hashtable', @ht2 OUT

    DECLARE @sb2 int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb2 OUT

    EXEC sp_OAMethod @sb2, 'LoadFile', @success OUT, 'qa_data/outlook/folderMap.xml', 'utf-8'
    EXEC sp_OAMethod @ht2, 'AddFromXmlSb', @success OUT, @sb2

    -- What's the ID for the folder "/Inbox/abc/subFolderA" ?

    EXEC sp_OAMethod @ht2, 'LookupStr', @sTmp0 OUT, '/Inbox/abc/subFolderA'
    PRINT 'id for /Inbox/abc/subFolderA = ' + @sTmp0

    -- 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.

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @folderMap
    EXEC @hr = sp_OADestroy @sbResponse
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @idQueue
    EXEC @hr = sp_OADestroy @sbFolderPath
    EXEC @hr = sp_OADestroy @sbQueueEntry
    EXEC @hr = sp_OADestroy @sbFolderMapXml
    EXEC @hr = sp_OADestroy @ht2
    EXEC @hr = sp_OADestroy @sb2


END
GO

 

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