Sample code for 30+ languages & platforms
SQL Server

BatchModify - Add a Label to each Message in Search Results

See more GMail REST API Examples

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

Chilkat SQL Server Downloads

SQL Server
-- 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)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    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
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, @url, @sb
    IF @success = 0
      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.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.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 @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @url, @json2, 'application/json', @resp
    IF @success = 0
      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
        EXEC @hr = sp_OADestroy @resp
        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 @http
        EXEC @hr = sp_OADestroy @sb
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @json2
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    -- The 204 response has an empty response body..


    PRINT 'BatchModify success!'

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


END
GO