Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

MS Graph Calendar Create Event

See more Microsoft Calendar Examples

Creates a new event in a specified calendar.

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

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  jsonToken: TJsonObject;
  json: TJsonObject;
  resp: THttpResponse;
  displayName: string;
  locationType: string;
  uniqueId: string;
  uniqueIdType: string;
  statusResponse: string;
  statusTime: string;
  emailAddressName: string;
  emailAddressAddress: string;
  odataContext: string;
  odataEtag: string;
  id: string;
  createdDateTime: string;
  lastModifiedDateTime: string;
  changeKey: string;
  originalStartTimeZone: string;
  originalEndTimeZone: string;
  iCalUId: string;
  reminderMinutesBeforeStart: string;
  isReminderOn: string;
  hasAttachments: string;
  subject: string;
  bodyPreview: string;
  importance: string;
  sensitivity: string;
  isAllDay: string;
  isCancelled: string;
  isOrganizer: string;
  responseRequested: string;
  seriesMasterId: string;
  showAs: string;
  type: string;
  webLink: string;
  onlineMeetingUrl: string;
  recurrence: 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 as shown here:
  //     Get Microsoft Graph OAuth2 Access Token with Calendars.ReadWrite scope.

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

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

  //  Create a JSON body for the HTTP POST
  //  Use this online tool to generate the code from sample JSON: 
  //  Generate Code to Create JSON

  //  {
  //    "subject": "Let's go for lunch",
  //    "body": {
  //      "contentType": "HTML",
  //      "content": "Does mid month work for you?"
  //    },
  //    "start": {
  //        "dateTime": "2019-11-15T12:00:00",
  //        "timeZone": "Pacific Standard Time"
  //    },
  //    "end": {
  //        "dateTime": "2019-11-15T14:00:00",
  //        "timeZone": "Pacific Standard Time"
  //    },
  //    "location":{
  //        "displayName":"Harry's Bar"
  //    },
  //    "attendees": [
  //      {
  //        "emailAddress": {
  //          "address":"adelev@contoso.onmicrosoft.com",
  //          "name": "Adele Vance"
  //        },
  //        "type": "required"
  //      }
  //    ]
  //  }

  json := TJsonObject.Create;
  json.UpdateString('subject','Let''s go for lunch');
  json.UpdateString('body.contentType','HTML');
  json.UpdateString('body.content','Does mid month work for you?');
  json.UpdateString('start.dateTime','2019-11-15T12:00:00');
  json.UpdateString('start.timeZone','Pacific Standard Time');
  json.UpdateString('end.dateTime','2019-11-15T14:00:00');
  json.UpdateString('end.timeZone','Pacific Standard Time');
  json.UpdateString('location.displayName','Harry''s Bar');
  json.UpdateString('attendees[0].emailAddress.address','adelev@contoso.onmicrosoft.com');
  json.UpdateString('attendees[0].emailAddress.name','Adele Vance');
  json.UpdateString('attendees[0].type','required');

  //  POST the JSON to https://graph.microsoft.com/v1.0/me/calendars/{id}/events
  //  This is posting to a calendar in the default calendarGroup.

  //  Specify the calendar id
  http.SetUrlVar('id','AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEGAAAA5_vF7TKKdE6bGCRqXyl2PQAClEpRTgAAAA==');
  resp := THttpResponse.Create;
  success := http.HttpJson('POST','https://graph.microsoft.com/v1.0/me/calendars/{$id}/events',json,'application/json',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  json.Load(resp.BodyStr);
  json.EmitCompact := False;

  if (resp.StatusCode <> 201) then
    begin
      WriteLn(json.Emit());
      WriteLn('Failed, response status code = ' + resp.StatusCode);
      Exit;
    end;

  WriteLn(json.Emit());

  //  A sample response:
  //  (See code for parsing this response below..)

  //  {
  //    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('admin%40chilkat.io')/calendars('AQMkADAwATM0MDAAMS ... AClEpRTgAAAA%3D%3D')/events/$entity",
  //    "@odata.etag": "W/\"5+vF7TKKdE6bGCRqXyl2PQAClIgmmw==\"",
  //    "id": "AQMkADAwAT ... ApRZ7JkAAAA=",
  //    "createdDateTime": "2019-04-26T14:31:39.8791929Z",
  //    "lastModifiedDateTime": "2019-04-26T14:31:41.2753537Z",
  //    "changeKey": "5+vF7TKKdE6bGCRqXyl2PQAClIgmmw==",
  //    "categories": [
  //    ],
  //    "originalStartTimeZone": "Pacific Standard Time",
  //    "originalEndTimeZone": "Pacific Standard Time",
  //    "iCalUId": "040000008200E00074C5B7101A82E00800000000F05DF1C23CFCD40100000000000000001000000009911D155F71EF42A230FEBFE5F7486A",
  //    "reminderMinutesBeforeStart": 15,
  //    "isReminderOn": true,
  //    "hasAttachments": false,
  //    "subject": "Let's go for lunch",
  //    "bodyPreview": "Does mid month 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=AQMkADAwATM0MDAAMS1iNTcwL...pRZ7JkAAAA%3D&exvsurl=1&path=/calendar/item",
  //    "onlineMeetingUrl": null,
  //    "recurrence": 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 mid month work for you?\r\n</body>\r\n</html>\r\n"
  //    },
  //    "start": {
  //      "dateTime": "2019-11-15T12:00:00.0000000",
  //      "timeZone": "Pacific Standard Time"
  //    },
  //    "end": {
  //      "dateTime": "2019-11-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": "Adele Vance",
  //          "address": "adelev@contoso.onmicrosoft.com"
  //        }
  //      }
  //    ],
  //    "organizer": {
  //      "emailAddress": {
  //        "name": "Matt",
  //        "address": "outlook_3A33FCEB9B74CC15@outlook.com"
  //      }
  //    }
  //  }
  //  
  //  

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

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

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

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

  WriteLn('Success.');


  http.Free;
  jsonToken.Free;
  json.Free;
  resp.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.