Sample code for 30+ languages & platforms
Lazarus Pascal

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 Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.HttpResponse,
  Chilkat.JsonObject,
  Chilkat.Crypt2;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  jsonToken: TJsonObject;
  json: TJsonObject;
  crypt: TCrypt2;
  resp: THttpResponse;
  jResp: TJsonObject;
  displayName: string;
  locationType: string;
  uniqueId: string;
  uniqueIdType: string;
  statusResponse: string;
  statusTime: string;
  emailAddressName: string;
  emailAddressAddress: string;
  odata_context: string;
  odata_etag: string;
  id: string;
  createdDateTime: string;
  lastModifiedDateTime: string;
  changeKey: string;
  transactionId: string;
  originalStartTimeZone: string;
  originalEndTimeZone: string;
  iCalUId: string;
  reminderMinutesBeforeStart: Integer;
  isReminderOn: Boolean;
  hasAttachments: Boolean;
  subject: string;
  bodyPreview: string;
  importance: string;
  sensitivity: string;
  isAllDay: Boolean;
  isCancelled: Boolean;
  isOrganizer: Boolean;
  responseRequested: Boolean;
  seriesMasterId: string;
  showAs: string;
  v_type: string;
  webLink: string;
  onlineMeetingUrl: string;
  isOnlineMeeting: Boolean;
  onlineMeetingProvider: string;
  allowNewTimeProposals: Boolean;
  isDraft: Boolean;
  hideAttendees: Boolean;
  recurrence: string;
  onlineMeeting: string;
  responseStatusResponse: string;
  responseStatusTime: string;
  bodyContentType: string;
  bodyContent: string;
  startDateTime: string;
  startTimeZone: string;
  endDateTime: string;
  endTimeZone: string;
  locationDisplayName: string;
  locationLocationType: string;
  locationUniqueId: string;
  locationUniqueIdType: string;
  organizerEmailAddressName: string;
  organizerEmailAddressAddress: string;
  i: Integer;
  count_i: Integer;

begin
  success := False;

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

  http := THttp.Create;

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

  jsonToken := TJsonObject.Create;
  success := jsonToken.LoadFile('qa_data/tokens/outlookCalendar.json');
  if (success = False) then
    begin
      WriteLn(jsonToken.LastErrorText);
      Exit;
    end;

  http.AuthToken := jsonToken.StringOf('access_token');

  //  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.
  json := TJsonObject.Create;
  json.UpdateString('subject','Let''s go for lunch');
  json.UpdateString('body.contentType','HTML');
  json.UpdateString('body.content','Does noon work for you?');
  json.UpdateString('start.dateTime','2021-05-15T12:00:00');
  json.UpdateString('start.timeZone','Pacific Standard Time');
  json.UpdateString('end.dateTime','2021-05-15T14:00:00');
  json.UpdateString('end.timeZone','Pacific Standard Time');
  json.UpdateString('location.displayName','Harry''s Bar');
  json.UpdateString('attendees[0].emailAddress.address','samanthab@contoso.onmicrosoft.com');
  json.UpdateString('attendees[0].emailAddress.name','Samantha Booth');
  json.UpdateString('attendees[0].type','required');
  json.UpdateBool('allowNewTimeProposals',True);

  //  Generate a UUID.
  crypt := TCrypt2.Create;
  json.UpdateString('transactionId',crypt.GenerateUuid());

  //  Add the "Prefer" request header.
  http.SetRequestHeader('Prefer','outlook.timezone="Pacific Standard Time"');

  //  Send the HTTP POST
  resp := THttpResponse.Create;
  success := http.HttpJson('POST','https://graph.microsoft.com/v1.0/me/events',json,'application/json',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  WriteLn('Response status code = ' + resp.StatusCode);

  jResp := TJsonObject.Create;
  jResp.Load(resp.BodyStr);
  jResp.EmitCompact := False;
  WriteLn(jResp.Emit());

  //  The send succeeded if the response status code = 201.
  if (resp.StatusCode <> 201) then
    begin
      WriteLn('Failed');
      Exit;
    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

  odata_context := jResp.StringOf('"@odata.context"');
  odata_etag := jResp.StringOf('"@odata.etag"');
  id := jResp.StringOf('id');
  createdDateTime := jResp.StringOf('createdDateTime');
  lastModifiedDateTime := jResp.StringOf('lastModifiedDateTime');
  changeKey := jResp.StringOf('changeKey');
  transactionId := jResp.StringOf('transactionId');
  originalStartTimeZone := jResp.StringOf('originalStartTimeZone');
  originalEndTimeZone := jResp.StringOf('originalEndTimeZone');
  iCalUId := jResp.StringOf('iCalUId');
  reminderMinutesBeforeStart := jResp.IntOf('reminderMinutesBeforeStart');
  isReminderOn := jResp.BoolOf('isReminderOn');
  hasAttachments := jResp.BoolOf('hasAttachments');
  subject := jResp.StringOf('subject');
  bodyPreview := jResp.StringOf('bodyPreview');
  importance := jResp.StringOf('importance');
  sensitivity := jResp.StringOf('sensitivity');
  isAllDay := jResp.BoolOf('isAllDay');
  isCancelled := jResp.BoolOf('isCancelled');
  isOrganizer := jResp.BoolOf('isOrganizer');
  responseRequested := jResp.BoolOf('responseRequested');
  seriesMasterId := jResp.StringOf('seriesMasterId');
  showAs := jResp.StringOf('showAs');
  v_type := jResp.StringOf('type');
  webLink := jResp.StringOf('webLink');
  onlineMeetingUrl := jResp.StringOf('onlineMeetingUrl');
  isOnlineMeeting := jResp.BoolOf('isOnlineMeeting');
  onlineMeetingProvider := jResp.StringOf('onlineMeetingProvider');
  allowNewTimeProposals := jResp.BoolOf('allowNewTimeProposals');
  isDraft := jResp.BoolOf('isDraft');
  hideAttendees := jResp.BoolOf('hideAttendees');
  recurrence := jResp.StringOf('recurrence');
  onlineMeeting := jResp.StringOf('onlineMeeting');
  responseStatusResponse := jResp.StringOf('responseStatus.response');
  responseStatusTime := jResp.StringOf('responseStatus.time');
  bodyContentType := jResp.StringOf('body.contentType');
  bodyContent := jResp.StringOf('body.content');
  startDateTime := jResp.StringOf('start.dateTime');
  startTimeZone := jResp.StringOf('start.timeZone');
  endDateTime := jResp.StringOf('end.dateTime');
  endTimeZone := jResp.StringOf('end.timeZone');
  locationDisplayName := jResp.StringOf('location.displayName');
  locationLocationType := jResp.StringOf('location.locationType');
  locationUniqueId := jResp.StringOf('location.uniqueId');
  locationUniqueIdType := jResp.StringOf('location.uniqueIdType');
  organizerEmailAddressName := jResp.StringOf('organizer.emailAddress.name');
  organizerEmailAddressAddress := jResp.StringOf('organizer.emailAddress.address');
  i := 0;
  count_i := jResp.SizeOfArray('categories');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('locations');
  while i < count_i do
    begin
      jResp.I := i;
      displayName := jResp.StringOf('locations[i].displayName');
      locationType := jResp.StringOf('locations[i].locationType');
      uniqueId := jResp.StringOf('locations[i].uniqueId');
      uniqueIdType := jResp.StringOf('locations[i].uniqueIdType');
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('attendees');
  while i < count_i do
    begin
      jResp.I := i;
      v_type := jResp.StringOf('attendees[i].type');
      statusResponse := jResp.StringOf('attendees[i].status.response');
      statusTime := jResp.StringOf('attendees[i].status.time');
      emailAddressName := jResp.StringOf('attendees[i].emailAddress.name');
      emailAddressAddress := jResp.StringOf('attendees[i].emailAddress.address');
      i := i + 1;
    end;

  WriteLn('Event created.');


  http.Free;
  jsonToken.Free;
  json.Free;
  crypt.Free;
  resp.Free;
  jResp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.