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

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 Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Use your previously obtained access token as shown here:
        //    Get Microsoft Graph OAuth2 Access Token with Calendars.ReadWrite scope.

        JsonObject jsonToken = new JsonObject();
        try {
            jsonToken.loadFile("qa_data/tokens/msGraphCalendar.json");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        http.setAuthToken(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"
        //     }
        //   ]
        // }

        JsonObject json = new JsonObject();
        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==");
        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "https://graph.microsoft.com/v1.0/me/calendars/{$id}/events", json, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        json.load(resp.getBodyStr());
        json.setEmitCompact(false);

        if (resp.getStatusCode() != 201) {
            System.out.println(json.emit());
            System.out.println("Failed, response status code = " + resp.getStatusCode());
            return;
        }

        System.out.println(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

        String displayName = "";
        String locationType = "";
        String uniqueId = "";
        String uniqueIdType = "";
        String statusResponse = "";
        String statusTime = "";
        String emailAddressName = "";
        String emailAddressAddress = "";

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

        i = 0;
        count_i = json.sizeOfArray("locations");
        while (i < count_i) {
            json.setI(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;
        }

        i = 0;
        count_i = json.sizeOfArray("attendees");
        while (i < count_i) {
            json.setI(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;
        }

        System.out.println("Success.");
    }
}