Java
Chilkat2 Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
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 here: Get Outlook Calendar OAuth2 Access Token (Azure AD v2.0 Endpoint).
JsonObject jsonToken = new JsonObject();
try {
jsonToken.loadFile("qa_data/tokens/outlookCalendar.json");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
http.setAuthToken(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.
JsonObject json = new JsonObject();
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.
Crypt2 crypt = new Crypt2();
json.updateString("transactionId", crypt.generateUuid());
// Add the "Prefer" request header.
http.setRequestHeader("Prefer", "outlook.timezone=\"Pacific Standard Time\"");
// Send the HTTP POST
HttpResponse resp = new HttpResponse();
try {
http.httpJson("POST", "https://graph.microsoft.com/v1.0/me/events", json, "application/json", resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Response status code = " + resp.getStatusCode());
JsonObject jResp = new JsonObject();
jResp.load(resp.getBodyStr());
jResp.setEmitCompact(false);
System.out.println(jResp.emit());
// The send succeeded if the response status code = 201.
if (resp.getStatusCode() != 201) {
System.out.println("Failed");
return;
}
// 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
String displayName = "";
String locationType = "";
String uniqueId = "";
String uniqueIdType = "";
String statusResponse = "";
String statusTime = "";
String emailAddressName = "";
String emailAddressAddress = "";
String odata_context = jResp.stringOf("\"@odata.context\"");
String odata_etag = jResp.stringOf("\"@odata.etag\"");
String id = jResp.stringOf("id");
String createdDateTime = jResp.stringOf("createdDateTime");
String lastModifiedDateTime = jResp.stringOf("lastModifiedDateTime");
String changeKey = jResp.stringOf("changeKey");
String transactionId = jResp.stringOf("transactionId");
String originalStartTimeZone = jResp.stringOf("originalStartTimeZone");
String originalEndTimeZone = jResp.stringOf("originalEndTimeZone");
String iCalUId = jResp.stringOf("iCalUId");
int reminderMinutesBeforeStart = jResp.intOf("reminderMinutesBeforeStart");
boolean isReminderOn = jResp.boolOf("isReminderOn");
boolean hasAttachments = jResp.boolOf("hasAttachments");
String subject = jResp.stringOf("subject");
String bodyPreview = jResp.stringOf("bodyPreview");
String importance = jResp.stringOf("importance");
String sensitivity = jResp.stringOf("sensitivity");
boolean isAllDay = jResp.boolOf("isAllDay");
boolean isCancelled = jResp.boolOf("isCancelled");
boolean isOrganizer = jResp.boolOf("isOrganizer");
boolean responseRequested = jResp.boolOf("responseRequested");
String seriesMasterId = jResp.stringOf("seriesMasterId");
String showAs = jResp.stringOf("showAs");
String v_type = jResp.stringOf("type");
String webLink = jResp.stringOf("webLink");
String onlineMeetingUrl = jResp.stringOf("onlineMeetingUrl");
boolean isOnlineMeeting = jResp.boolOf("isOnlineMeeting");
String onlineMeetingProvider = jResp.stringOf("onlineMeetingProvider");
boolean allowNewTimeProposals = jResp.boolOf("allowNewTimeProposals");
boolean isDraft = jResp.boolOf("isDraft");
boolean hideAttendees = jResp.boolOf("hideAttendees");
String recurrence = jResp.stringOf("recurrence");
String onlineMeeting = jResp.stringOf("onlineMeeting");
String responseStatusResponse = jResp.stringOf("responseStatus.response");
String responseStatusTime = jResp.stringOf("responseStatus.time");
String bodyContentType = jResp.stringOf("body.contentType");
String bodyContent = jResp.stringOf("body.content");
String startDateTime = jResp.stringOf("start.dateTime");
String startTimeZone = jResp.stringOf("start.timeZone");
String endDateTime = jResp.stringOf("end.dateTime");
String endTimeZone = jResp.stringOf("end.timeZone");
String locationDisplayName = jResp.stringOf("location.displayName");
String locationLocationType = jResp.stringOf("location.locationType");
String locationUniqueId = jResp.stringOf("location.uniqueId");
String locationUniqueIdType = jResp.stringOf("location.uniqueIdType");
String organizerEmailAddressName = jResp.stringOf("organizer.emailAddress.name");
String organizerEmailAddressAddress = jResp.stringOf("organizer.emailAddress.address");
int i = 0;
int count_i = jResp.sizeOfArray("categories");
while (i < count_i) {
jResp.setI(i);
i = i + 1;
}
i = 0;
count_i = jResp.sizeOfArray("locations");
while (i < count_i) {
jResp.setI(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;
}
i = 0;
count_i = jResp.sizeOfArray("attendees");
while (i < count_i) {
jResp.setI(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;
}
System.out.println("Event created.");
}
}