Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

MS Graph Create Calendar

See more Microsoft Calendar Examples

Creates a new calendar.

For more details, see https://docs.microsoft.com/en-us/graph/api/user-post-calendars?view=graph-rest-1.0

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
    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 as shown here:
    --     Get Microsoft Graph OAuth2 Access Token with Calendars.ReadWrite scope.

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

    EXEC sp_OAMethod @jsonToken, 'LoadFile', @success OUT, 'qa_data/tokens/msGraphCalendar.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

    --  Create a JSON body for the HTTP POST
    --  {
    --    "name": "Work"
    --  }
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name', 'Work'

    --  POST the JSON to https://graph.microsoft.com/v1.0/me/calendars
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://graph.microsoft.com/v1.0/me/calendars', @json, '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 @jsonToken
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

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

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 <> 201
      BEGIN
        EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
        PRINT @sTmp0

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'Failed, response status code = ' + @iTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonToken
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    --  A sample response:
    --  {
    --    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('admin%40chilkat.io')/calendars/$entity",
    --    "id": "AQMkAD...TgAAAA==",
    --    "name": "Work",
    --    "color": "auto",
    --    "changeKey": "5+vF7T...HjDcA==",
    --    "canShare": true,
    --    "canViewPrivateItems": true,
    --    "canEdit": true,
    --    "owner": {
    --      "name": "...",
    --      "address": "outlook_3A33...4CC15@outlook.com"
    --    }
    --  }

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

    DECLARE @odataContext nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @odataContext OUT, '"@odata.context"'
    DECLARE @id nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @id OUT, 'id'
    DECLARE @name nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'name'
    DECLARE @color nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @color OUT, 'color'
    DECLARE @changeKey nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @changeKey OUT, 'changeKey'
    DECLARE @canShare nvarchar(4000)
    EXEC sp_OAMethod @json, 'BoolOf', @canShare OUT, 'canShare'
    DECLARE @canViewPrivateItems nvarchar(4000)
    EXEC sp_OAMethod @json, 'BoolOf', @canViewPrivateItems OUT, 'canViewPrivateItems'
    DECLARE @canEdit nvarchar(4000)
    EXEC sp_OAMethod @json, 'BoolOf', @canEdit OUT, 'canEdit'
    DECLARE @ownerName nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @ownerName OUT, 'owner.name'
    DECLARE @ownerAddress nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @ownerAddress OUT, 'owner.address'


    PRINT 'Success.'

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


END
GO