Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) BatchModify - Add a Label to each Message in Search ResultsSearchs GMail for messages meeting a criteria and adds a label to each message found.
-- 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 @success int 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 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 -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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 -- 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, @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 -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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-2024 Chilkat Software, Inc. All Rights Reserved.