Outlook Calendar: Create an Appointment with a Transaction ID
See more Outlook Calendar Examples
Create a one-hour appointment 24 hours from the current time in the default calendar. Requires delegated Calendars.ReadWrite. The subject and body are generic, times are computed at runtime, and no attendees are included. Adding attendees changes this to a meeting and Graph sends invitations; setting a location name alone does not reserve a room.
The example saves the request and its transactionId before sending, then records the returned event ID in a receipt. After an uncertain response, reuse the same payload and transaction ID; do not generate a new UUID for each retry. A receipt prevents an already-successful demonstration run from posting again. For a deliberately new event, use a new pair of request/receipt filenames and a new transaction ID. Keep each operation tied to the same mailbox and serialize access. Production outboxes need durable, atomic storage; this sample does not implement a general retry scheduler or promise indefinite deduplication.
Input dateTime values are paired with timeZone="UTC". For local wall-clock scheduling, choose a mailbox-supported zone and handle daylight-saving transitions explicitly. The response preference requests UTC display and immutable event IDs; preserve the returned ID as a case-sensitive string for later reads or deletion. HTTP 201 confirms creation. A local receipt-write failure does not undo the server operation.
Uses Microsoft Graph v1.0 in the global cloud with a delegated access token saved by the calendar OAuth2 example. Both personal Outlook.com accounts and Microsoft 365 work/school accounts with a mailbox can use this flow. The app registration must support the selected account type. A token for SMTP/IMAP/POP or another API is not a Graph token.
/me means the signed-in user and requires delegated authentication. An unattended Microsoft 365 service instead needs application permissions and administrator consent, and addresses an explicit mailbox through /users/{id-or-userPrincipalName}. That client-credentials flow does not apply to personal Outlook.com accounts. These samples target the global cloud; national clouds use different authority and Graph hosts.
HTTP methods can return false for an HTTP error as well as a connection failure. The code distinguishes those cases and checks the expected status before reading JSON. For 401, refresh the access token or authorize again; for 403, check permissions, mailbox access, and tenant policy. For 429, honor Retry-After before a bounded retry. Do not automatically repeat a write after an uncertain outcome. Keep detailed errors and calendar data private.
Chilkat SQL Server Downloads
-- 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 @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @sTmp1 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
SELECT @success = 0
-- Obtain a delegated Microsoft Graph token using the calendar authorization example.
-- See Authorize Outlook Calendar with OAuth2 and PKCE.
DECLARE @token int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @token, 'Strict', 1
EXEC sp_OAMethod @token, 'LoadFile', @success OUT, 'qa_data/tokens/outlookCalendar.json'
IF @success <> 1
BEGIN
PRINT 'Could not read the private calendar token cache.'
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @token, 'StringOfEquals', @iTmp1 OUT, 'access_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty access_token in the JSON.'
EXEC @hr = sp_OADestroy @token
RETURN
END
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'FollowRedirects', 0
EXEC sp_OASetProperty @http, 'ConnectTimeout', 20
EXEC sp_OASetProperty @http, 'ReadTimeout', 30
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
EXEC sp_OASetProperty @result, 'Strict', 1
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Prefer', 'outlook.timezone="UTC", IdType="ImmutableId"'
-- Live write: creates an appointment in the signed-in user's default calendar.
-- Keep one request file per logical operation and account. Do not run concurrent copies.
DECLARE @requestFile nvarchar(4000)
SELECT @requestFile = 'qa_data/outlookCalendar/create-event-request.json'
DECLARE @resultFile nvarchar(4000)
SELECT @resultFile = 'qa_data/outlookCalendar/created-event.json'
DECLARE @fac int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
EXEC sp_OAMethod @fac, 'DirEnsureExists', @success OUT, 'qa_data/outlookCalendar'
IF @success <> 1
BEGIN
PRINT 'Could not create the private operation directory.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
RETURN
END
DECLARE @body int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT
EXEC sp_OASetProperty @body, 'Strict', 1
EXEC sp_OAMethod @fac, 'FileExists', @iTmp0 OUT, @requestFile
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @body, 'LoadFile', @success OUT, @requestFile
IF @success <> 1
BEGIN
PRINT 'Could not read the saved create request; nothing sent.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
RETURN
END
-- Retry the same saved body and transactionId after an uncertain result.
END
ELSE
BEGIN
DECLARE @when int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @when OUT
EXEC sp_OAMethod @when, 'SetFromCurrentSystemTime', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not get the current time.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
RETURN
END
EXEC sp_OAMethod @when, 'AddSeconds', @success OUT, 86400
IF @success <> 1
BEGIN
PRINT 'Could not calculate the event start.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'subject', 'Sample appointment'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'body.contentType', 'text'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'body.content', 'Sample event details.'
EXEC sp_OAMethod @when, 'GetAsIso8601', @sTmp0 OUT, 'YYYY-MM-DDThh:mm:ss', 0
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'start.dateTime', @sTmp0
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'start.timeZone', 'UTC'
EXEC sp_OAMethod @when, 'AddSeconds', @success OUT, 3600
IF @success <> 1
BEGIN
PRINT 'Could not calculate the event end.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
RETURN
END
EXEC sp_OAMethod @when, 'GetAsIso8601', @sTmp0 OUT, 'YYYY-MM-DDThh:mm:ss', 0
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'end.dateTime', @sTmp0
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'end.timeZone', 'UTC'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'showAs', 'busy'
EXEC sp_OAMethod @body, 'UpdateBool', @success OUT, 'isReminderOn', 1
EXEC sp_OAMethod @body, 'UpdateInt', @success OUT, 'reminderMinutesBeforeStart', 15
-- No attendees: this is an appointment, so the request does not send invitations.
DECLARE @crypt int
EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT
DECLARE @transactionId nvarchar(4000)
EXEC sp_OAMethod @crypt, 'GenerateUuid', @transactionId OUT
EXEC sp_OAGetProperty @crypt, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Could not create a transaction ID.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'transactionId', @transactionId
EXEC sp_OAMethod @body, 'WriteFile', @success OUT, @requestFile
IF @success <> 1
BEGIN
PRINT 'Could not save the request before sending; nothing sent.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
RETURN
END
END
EXEC sp_OAMethod @body, 'JsonTypeOf', @iTmp0 OUT, 'transactionId'
EXEC sp_OAMethod @body, 'StringOfEquals', @iTmp1 OUT, 'transactionId', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty transactionId in the JSON.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
RETURN
END
-- A previous successful run leaves a small receipt. Do not submit it again.
EXEC sp_OAMethod @fac, 'FileExists', @iTmp0 OUT, @resultFile
IF @iTmp0 = 1
BEGIN
DECLARE @receipt int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @receipt OUT
EXEC sp_OASetProperty @receipt, 'Strict', 1
EXEC sp_OAMethod @receipt, 'LoadFile', @success OUT, @resultFile
IF @success <> 1
BEGIN
PRINT 'Could not read the existing receipt; nothing sent.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'JsonTypeOf', @iTmp0 OUT, 'id'
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp1 OUT, 'id', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty id in the JSON.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @body, 'StringOf', @sTmp0 OUT, 'transactionId'
EXEC sp_OAMethod @receipt, 'StringOfEquals', @iTmp0 OUT, 'transactionId', @sTmp0, 1
IF @iTmp0 <> 1
BEGIN
PRINT 'The receipt belongs to another operation; nothing sent.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @receipt, 'StringOf', @sTmp0 OUT, 'id'
PRINT 'Already created event ID: ' + @sTmp0
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://graph.microsoft.com/v1.0/me/events', @body, 'application/json', @resp
-- A false result can still include an HTTP error response.
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) and (@iTmp0 = 0)
BEGIN
PRINT 'No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 201
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Unexpected HTTP status: ' + @iTmp0
EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'request-id'
PRINT 'Request ID: ' + @sTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 429
BEGIN
EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
PRINT 'Retry-After: ' + @sTmp0
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 401
BEGIN
PRINT 'Refresh the Graph access token or authorize again.'
END
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Expected a well-formed JSON response.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @receipt
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'id'
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp1 OUT, 'id', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty id in the JSON.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @receipt
RETURN
END
DECLARE @saved int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @saved OUT
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'id'
EXEC sp_OAMethod @saved, 'UpdateString', @success OUT, 'id', @sTmp0
EXEC sp_OAMethod @body, 'StringOf', @sTmp0 OUT, 'transactionId'
EXEC sp_OAMethod @saved, 'UpdateString', @success OUT, 'transactionId', @sTmp0
-- Display the ID even if saving the receipt fails: the server has already created the event.
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'id'
PRINT 'Created event ID: ' + @sTmp0
EXEC sp_OAMethod @saved, 'WriteFile', @success OUT, @resultFile
IF @success <> 1
BEGIN
PRINT 'The event was created, but its receipt could not be saved. Reconcile before retrying.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @saved
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'start.dateTime'
EXEC sp_OAMethod @result, 'StringOf', @sTmp1 OUT, 'start.timeZone'
PRINT 'Start: ' + @sTmp0 + ' (' + @sTmp1 + ')'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'end.dateTime'
EXEC sp_OAMethod @result, 'StringOf', @sTmp1 OUT, 'end.timeZone'
PRINT 'End: ' + @sTmp0 + ' (' + @sTmp1 + ')'
PRINT 'Event receipt saved.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @when
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @receipt
EXEC @hr = sp_OADestroy @saved
END
GO