Sample code for 30+ languages & platforms
SQL Server

Outlook Calendar List Calendars

See more Outlook Calendar Examples

Get all the user's calendars.

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

    -- Use your previously obtained access token here: Get Outlook Calendar OAuth2 Access Token (Azure AD v2.0 Endpoint).

    DECLARE @jsonToken int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonToken OUT

    EXEC sp_OAMethod @jsonToken, 'LoadFile', @success OUT, 'qa_data/tokens/outlookCalendar.json'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @jsonToken, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonToken
        RETURN
      END

    EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'access_token'
    EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', 'https://graph.microsoft.com/v1.0/me/calendars', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonToken
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END


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

    -- The HTTP request succeeded if the response status code = 200.
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonToken
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

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

    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @json, 'Load', @success OUT, @sTmp0
    EXEC sp_OASetProperty @json, 'EmitCompact', 0
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- Here is a sample response:

    -- Use this online tool to generate parsing code from sample JSON: 
    -- Generate Parsing Code from JSON

    -- {
    --     "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#me/calendars",
    --     "value": [
    --         {
    --             "@odata.id": "https://graph.microsoft.com/v1.0/users('ddfcd489-628b-40d7-b48b-57002df800e5@1717622f-1d94-4d0c-9d74-709fad664b77')/calendars('AAMkAGI2TGuLAAA=')",
    --             "id": "AAMkAGI2TGuLAAA=",
    --             "name": "Calendar",
    --             "color": "auto",
    --             "changeKey": "nfZyf7VcrEKLNoU37KWlkQAAA0x0+w==",
    --             "canShare":true,
    --             "canViewPrivateItems":true,
    --             "hexColor": "",
    --             "canEdit":true,
    --             "allowedOnlineMeetingProviders": [
    --                 "teamsForBusiness"
    --             ],
    --             "defaultOnlineMeetingProvider": "teamsForBusiness",
    --             "isTallyingResponses": true,
    --             "isRemovable": false,
    --             "owner":{
    --                 "name":"Samantha Booth",
    --                 "address":"samanthab@adatum.onmicrosoft.com"
    --             }
    --         }
    --     ]
    -- }

    DECLARE @id nvarchar(4000)

    DECLARE @name nvarchar(4000)

    DECLARE @color nvarchar(4000)

    DECLARE @hexColor nvarchar(4000)

    DECLARE @isDefaultCalendar int

    DECLARE @changeKey nvarchar(4000)

    DECLARE @canShare int

    DECLARE @canViewPrivateItems int

    DECLARE @canEdit int

    DECLARE @defaultOnlineMeetingProvider nvarchar(4000)

    DECLARE @isTallyingResponses int

    DECLARE @isRemovable int

    DECLARE @ownerName nvarchar(4000)

    DECLARE @ownerAddress nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @strVal nvarchar(4000)

    DECLARE @odata_context nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @odata_context OUT, '"@odata.context"'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'value'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @id OUT, 'value[i].id'
        EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'value[i].name'
        EXEC sp_OAMethod @json, 'StringOf', @color OUT, 'value[i].color'
        EXEC sp_OAMethod @json, 'StringOf', @hexColor OUT, 'value[i].hexColor'
        EXEC sp_OAMethod @json, 'BoolOf', @isDefaultCalendar OUT, 'value[i].isDefaultCalendar'
        EXEC sp_OAMethod @json, 'StringOf', @changeKey OUT, 'value[i].changeKey'
        EXEC sp_OAMethod @json, 'BoolOf', @canShare OUT, 'value[i].canShare'
        EXEC sp_OAMethod @json, 'BoolOf', @canViewPrivateItems OUT, 'value[i].canViewPrivateItems'
        EXEC sp_OAMethod @json, 'BoolOf', @canEdit OUT, 'value[i].canEdit'
        EXEC sp_OAMethod @json, 'StringOf', @defaultOnlineMeetingProvider OUT, 'value[i].defaultOnlineMeetingProvider'
        EXEC sp_OAMethod @json, 'BoolOf', @isTallyingResponses OUT, 'value[i].isTallyingResponses'
        EXEC sp_OAMethod @json, 'BoolOf', @isRemovable OUT, 'value[i].isRemovable'
        EXEC sp_OAMethod @json, 'StringOf', @ownerName OUT, 'value[i].owner.name'
        EXEC sp_OAMethod @json, 'StringOf', @ownerAddress OUT, 'value[i].owner.address'
        SELECT @j = 0
        EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'value[i].allowedOnlineMeetingProviders'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @json, 'J', @j
            EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'value[i].allowedOnlineMeetingProviders[j]'
            SELECT @j = @j + 1
          END
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @jsonToken
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @json


END
GO