Sample code for 30+ languages & platforms
SQL Server

Outlook Calendar Create an Event

See more Outlook Calendar Examples

Create an event in the specified time zone, and assign the event an optional transactionId value.

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

    -- Send the following POST:

    -- POST https://graph.microsoft.com/v1.0/me/events
    -- Prefer: outlook.timezone="Pacific Standard Time"
    -- Content-type: application/json
    -- 
    -- {
    --   "subject": "Let's go for lunch",
    --   "body": {
    --     "contentType": "HTML",
    --     "content": "Does noon work for you?"
    --   },
    --   "start": {
    --       "dateTime": "2017-04-15T12:00:00",
    --       "timeZone": "Pacific Standard Time"
    --   },
    --   "end": {
    --       "dateTime": "2017-04-15T14:00:00",
    --       "timeZone": "Pacific Standard Time"
    --   },
    --   "location":{
    --       "displayName":"Harry's Bar"
    --   },
    --   "attendees": [
    --     {
    --       "emailAddress": {
    --         "address":"samanthab@contoso.onmicrosoft.com",
    --         "name": "Samantha Booth"
    --       },
    --       "type": "required"
    --     }
    --   ],
    --   "allowNewTimeProposals": true,
    --   "transactionId":"7E163156-7762-4BEB-A1C6-729EA81755A7"
    -- }

    -- Build the JSON body of the POST.
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'subject', 'Let''s go for lunch'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'body.contentType', 'HTML'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'body.content', 'Does noon work for you?'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'start.dateTime', '2021-05-15T12:00:00'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'start.timeZone', 'Pacific Standard Time'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'end.dateTime', '2021-05-15T14:00:00'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'end.timeZone', 'Pacific Standard Time'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'location.displayName', 'Harry''s Bar'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'attendees[0].emailAddress.address', 'samanthab@contoso.onmicrosoft.com'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'attendees[0].emailAddress.name', 'Samantha Booth'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'attendees[0].type', 'required'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'allowNewTimeProposals', 1

    -- Generate a UUID.
    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT

    EXEC sp_OAMethod @crypt, 'GenerateUuid', @sTmp0 OUT
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'transactionId', @sTmp0

    -- Add the "Prefer" request header.
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Prefer', 'outlook.timezone="Pacific Standard Time"'

    -- Send the HTTP POST
    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/events', @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 @crypt
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END


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

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

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

    -- The send succeeded if the response status code = 201.
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 <> 201
      BEGIN

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

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('user%40example.com')/events/$entity",
    --   "@odata.etag": "W/\"5+vF7TKKdE6bGCRqXyl2PQAEaGQgcw==\"",
    --   "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgENAAAA5_vF7TKKdE6bGCRqXyl2PQAEaDkEcAAAAA==",
    --   "createdDateTime": "2021-04-18T23:38:52.1979259Z",
    --   "lastModifiedDateTime": "2021-04-18T23:38:53.3747647Z",
    --   "changeKey": "5+vF7TKKdE6bGCRqXyl2PQAEaGQgcw==",
    --   "categories": [
    --   ],
    --   "transactionId": "1d12b565-3ca3-4ed8-b3f9-e8a14ac3ac17",
    --   "originalStartTimeZone": "Pacific Standard Time",
    --   "originalEndTimeZone": "Pacific Standard Time",
    --   "iCalUId": "040000008200E00074C5B7101A82E00800000000EF0328FDAB34D7010000000000000000100000004478DD5948382543AFD1B52C25E88C02",
    --   "reminderMinutesBeforeStart": 15,
    --   "isReminderOn": true,
    --   "hasAttachments": false,
    --   "subject": "Let's go for lunch",
    --   "bodyPreview": "Does noon work for you?",
    --   "importance": "normal",
    --   "sensitivity": "normal",
    --   "isAllDay": false,
    --   "isCancelled": false,
    --   "isOrganizer": true,
    --   "responseRequested": true,
    --   "seriesMasterId": null,
    --   "showAs": "busy",
    --   "type": "singleInstance",
    --   "webLink": "https://outlook.live.com/owa/?itemid=AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5%2BvF7TKKdE6bGCRqXyl2PQAAAgENAAAA5%2BvF7TKKdE6bGCRqXyl2PQAEaDkEcAAAAA%3D%3D&exvsurl=1&path=/calendar/item",
    --   "onlineMeetingUrl": null,
    --   "isOnlineMeeting": false,
    --   "onlineMeetingProvider": "unknown",
    --   "allowNewTimeProposals": true,
    --   "isDraft": false,
    --   "hideAttendees": false,
    --   "recurrence": null,
    --   "onlineMeeting": null,
    --   "responseStatus": {
    --     "response": "organizer",
    --     "time": "0001-01-01T00:00:00Z"
    --   },
    --   "body": {
    --     "contentType": "html",
    --     "content": "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=us-ascii\">\r\n</head>\r\n<body>\r\nDoes noon work for you?\r\n</body>\r\n</html>\r\n"
    --   },
    --   "start": {
    --     "dateTime": "2021-05-15T12:00:00.0000000",
    --     "timeZone": "Pacific Standard Time"
    --   },
    --   "end": {
    --     "dateTime": "2021-05-15T14:00:00.0000000",
    --     "timeZone": "Pacific Standard Time"
    --   },
    --   "location": {
    --     "displayName": "Harry's Bar",
    --     "locationType": "default",
    --     "uniqueId": "Harry's Bar",
    --     "uniqueIdType": "private"
    --   },
    --   "locations": [
    --     {
    --       "displayName": "Harry's Bar",
    --       "locationType": "default",
    --       "uniqueId": "Harry's Bar",
    --       "uniqueIdType": "private"
    --     }
    --   ],
    --   "attendees": [
    --     {
    --       "type": "required",
    --       "status": {
    --         "response": "none",
    --         "time": "0001-01-01T00:00:00Z"
    --       },
    --       "emailAddress": {
    --         "name": "Samantha Booth",
    --         "address": "samanthab@contoso.onmicrosoft.com"
    --       }
    --     }
    --   ],
    --   "organizer": {
    --     "emailAddress": {
    --       "name": "John Doe",
    --       "address": "outlook_3A33FCEB9B74CC15@outlook.com"
    --     }
    --   }
    -- }

    -- Sample code for parsing the JSON response...
    -- Use the following online tool to generate parsing code from sample JSON:
    -- Generate Parsing Code from JSON

    DECLARE @displayName nvarchar(4000)

    DECLARE @locationType nvarchar(4000)

    DECLARE @uniqueId nvarchar(4000)

    DECLARE @uniqueIdType nvarchar(4000)

    DECLARE @statusResponse nvarchar(4000)

    DECLARE @statusTime nvarchar(4000)

    DECLARE @emailAddressName nvarchar(4000)

    DECLARE @emailAddressAddress nvarchar(4000)

    DECLARE @odata_context nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @odata_context OUT, '"@odata.context"'
    DECLARE @odata_etag nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @odata_etag OUT, '"@odata.etag"'
    DECLARE @id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'id'
    DECLARE @createdDateTime nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @createdDateTime OUT, 'createdDateTime'
    DECLARE @lastModifiedDateTime nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @lastModifiedDateTime OUT, 'lastModifiedDateTime'
    DECLARE @changeKey nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @changeKey OUT, 'changeKey'
    DECLARE @transactionId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @transactionId OUT, 'transactionId'
    DECLARE @originalStartTimeZone nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @originalStartTimeZone OUT, 'originalStartTimeZone'
    DECLARE @originalEndTimeZone nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @originalEndTimeZone OUT, 'originalEndTimeZone'
    DECLARE @iCalUId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @iCalUId OUT, 'iCalUId'
    DECLARE @reminderMinutesBeforeStart int
    EXEC sp_OAMethod @jResp, 'IntOf', @reminderMinutesBeforeStart OUT, 'reminderMinutesBeforeStart'
    DECLARE @isReminderOn int
    EXEC sp_OAMethod @jResp, 'BoolOf', @isReminderOn OUT, 'isReminderOn'
    DECLARE @hasAttachments int
    EXEC sp_OAMethod @jResp, 'BoolOf', @hasAttachments OUT, 'hasAttachments'
    DECLARE @subject nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @subject OUT, 'subject'
    DECLARE @bodyPreview nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @bodyPreview OUT, 'bodyPreview'
    DECLARE @importance nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @importance OUT, 'importance'
    DECLARE @sensitivity nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @sensitivity OUT, 'sensitivity'
    DECLARE @isAllDay int
    EXEC sp_OAMethod @jResp, 'BoolOf', @isAllDay OUT, 'isAllDay'
    DECLARE @isCancelled int
    EXEC sp_OAMethod @jResp, 'BoolOf', @isCancelled OUT, 'isCancelled'
    DECLARE @isOrganizer int
    EXEC sp_OAMethod @jResp, 'BoolOf', @isOrganizer OUT, 'isOrganizer'
    DECLARE @responseRequested int
    EXEC sp_OAMethod @jResp, 'BoolOf', @responseRequested OUT, 'responseRequested'
    DECLARE @seriesMasterId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @seriesMasterId OUT, 'seriesMasterId'
    DECLARE @showAs nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @showAs OUT, 'showAs'
    DECLARE @v_type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'type'
    DECLARE @webLink nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @webLink OUT, 'webLink'
    DECLARE @onlineMeetingUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @onlineMeetingUrl OUT, 'onlineMeetingUrl'
    DECLARE @isOnlineMeeting int
    EXEC sp_OAMethod @jResp, 'BoolOf', @isOnlineMeeting OUT, 'isOnlineMeeting'
    DECLARE @onlineMeetingProvider nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @onlineMeetingProvider OUT, 'onlineMeetingProvider'
    DECLARE @allowNewTimeProposals int
    EXEC sp_OAMethod @jResp, 'BoolOf', @allowNewTimeProposals OUT, 'allowNewTimeProposals'
    DECLARE @isDraft int
    EXEC sp_OAMethod @jResp, 'BoolOf', @isDraft OUT, 'isDraft'
    DECLARE @hideAttendees int
    EXEC sp_OAMethod @jResp, 'BoolOf', @hideAttendees OUT, 'hideAttendees'
    DECLARE @recurrence nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @recurrence OUT, 'recurrence'
    DECLARE @onlineMeeting nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @onlineMeeting OUT, 'onlineMeeting'
    DECLARE @responseStatusResponse nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @responseStatusResponse OUT, 'responseStatus.response'
    DECLARE @responseStatusTime nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @responseStatusTime OUT, 'responseStatus.time'
    DECLARE @bodyContentType nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @bodyContentType OUT, 'body.contentType'
    DECLARE @bodyContent nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @bodyContent OUT, 'body.content'
    DECLARE @startDateTime nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @startDateTime OUT, 'start.dateTime'
    DECLARE @startTimeZone nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @startTimeZone OUT, 'start.timeZone'
    DECLARE @endDateTime nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @endDateTime OUT, 'end.dateTime'
    DECLARE @endTimeZone nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @endTimeZone OUT, 'end.timeZone'
    DECLARE @locationDisplayName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @locationDisplayName OUT, 'location.displayName'
    DECLARE @locationLocationType nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @locationLocationType OUT, 'location.locationType'
    DECLARE @locationUniqueId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @locationUniqueId OUT, 'location.uniqueId'
    DECLARE @locationUniqueIdType nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @locationUniqueIdType OUT, 'location.uniqueIdType'
    DECLARE @organizerEmailAddressName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @organizerEmailAddressName OUT, 'organizer.emailAddress.name'
    DECLARE @organizerEmailAddressAddress nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @organizerEmailAddressAddress OUT, 'organizer.emailAddress.address'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'categories'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'locations'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @displayName OUT, 'locations[i].displayName'
        EXEC sp_OAMethod @jResp, 'StringOf', @locationType OUT, 'locations[i].locationType'
        EXEC sp_OAMethod @jResp, 'StringOf', @uniqueId OUT, 'locations[i].uniqueId'
        EXEC sp_OAMethod @jResp, 'StringOf', @uniqueIdType OUT, 'locations[i].uniqueIdType'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'attendees'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'attendees[i].type'
        EXEC sp_OAMethod @jResp, 'StringOf', @statusResponse OUT, 'attendees[i].status.response'
        EXEC sp_OAMethod @jResp, 'StringOf', @statusTime OUT, 'attendees[i].status.time'
        EXEC sp_OAMethod @jResp, 'StringOf', @emailAddressName OUT, 'attendees[i].emailAddress.name'
        EXEC sp_OAMethod @jResp, 'StringOf', @emailAddressAddress OUT, 'attendees[i].emailAddress.address'
        SELECT @i = @i + 1
      END


    PRINT 'Event created.'

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


END
GO