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

SQL Server Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
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)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(SQL Server) BatchModify - Add a Label to each Message in Search Results

Searchs GMail for messages meeting a criteria and adds a label to each message found.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @success int

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @http, 'AuthToken', 'GMAIL-ACCESS-TOKEN'

    DECLARE @userId nvarchar(4000)
    SELECT @userId = 'me'
    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'userId', @userId

    DECLARE @query nvarchar(4000)
    SELECT @query = 'subject:questions'
    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'query', @query

    DECLARE @url nvarchar(4000)
    SELECT @url = 'https://www.googleapis.com/gmail/v1/users/{$userId}/messages?q={$query}'

    DECLARE @sb int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sb OUT

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, @url, @sb
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sb
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'LoadSb', @success OUT, @sb
    EXEC sp_OASetProperty @json, 'EmitCompact', 0
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sb
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    -- If successful, the received JSON looks like this:
    -- {
    --   "messages": [
    --     {
    --       "id": "166f583051d36144",
    --       "threadId": "166f583051d36144"
    --     },
    --     {
    --       "id": "166f5815e1f36144",
    --       "threadId": "166f5815e1f36144"
    --     },
    -- 	...
    --     {
    --       "id": "166f580639e36144",
    --       "threadId": "166f580639e36144"
    --     },
    --     {
    --       "id": "15dbc2e28ec789c6",
    --       "threadId": "15dbc2e28ec789c6"
    --     }
    --   ],
    --   "nextPageToken": "13434766102274844688",
    --   "resultSizeEstimate": 103
    -- }
    -- 

    -- Next, we'll be sending an HTTP POST to add the label "questions" to each message in the
    -- search results.  The JSON to be sent for the batchModify is this:

    -- 	{
    -- 	  "ids": [
    -- 	    string
    -- 	  ],
    -- 	  "addLabelIds": [
    -- 	    string
    -- 	  ],
    -- 	  "removeLabelIds": [
    -- 	    string
    -- 	  ]
    -- 	}

    -- We'll omit "removeLabelIds" because we're not removing any labels.
    -- We are parsing the JSON search results, and at the same time building the batchModify JSON.

    DECLARE @json2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json2 OUT

    DECLARE @i int
    SELECT @i = 0
    DECLARE @numMessages int
    EXEC sp_OAMethod @json, 'SizeOfArray', @numMessages OUT, 'messages'
    WHILE (@i < @numMessages)
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        DECLARE @id nvarchar(4000)
        EXEC sp_OAMethod @json, 'StringOf', @id OUT, 'messages[i].id'
        EXEC sp_OASetProperty @json2, 'I', @i
        EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'ids[i]', @id
        SELECT @i = @i + 1
      END

    -- We need the id of the label (not the name).
    -- I know the name of the label is "questions", but I need to know the id.
    -- See this example: Get Label Id by Name
    -- The id of my label named "questions" is "Label_43"
    DECLARE @labelId nvarchar(4000)
    SELECT @labelId = 'Label_43'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'addLabelIds[0]', @labelId
    EXEC sp_OAMethod @json2, 'UpdateNewArray', @success OUT, 'removeLabelIds'

    EXEC sp_OASetProperty @json2, 'EmitCompact', 0
    EXEC sp_OAMethod @json2, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- Send the batchModify
    SELECT @url = 'https://www.googleapis.com/gmail/v1/users/{$userId}/messages/batchModify'
    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, @url, 'application/json', @json2
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sb
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @json2
        RETURN
      END


    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT 'status = ' + @iTmp0

    -- A 204 response status indicate success.
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 <> 204
      BEGIN
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sb
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @json2
        RETURN
      END

    -- The 204 response has an empty response body..
    EXEC @hr = sp_OADestroy @resp


    PRINT 'BatchModify success!'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @sb
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @json2


END
GO

 

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