Sample code for 30+ languages & platforms
Rust

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

Rust

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

let http = chilkat::Http::new();

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

let json_token = chilkat::JsonObject::new();
if json_token.load_file("qa_data/tokens/outlookCalendar.json").is_err() {
    println!("{}", json_token.last_error_text());
    return;
}

http.set_auth_token(&json_token.string_of("access_token").unwrap_or_default());

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

// Generate a UUID.
let crypt = chilkat::Crypt2::new();
let _ = json.update_string("transactionId", &crypt.generate_uuid().unwrap_or_default());

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

// Send the HTTP POST
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://graph.microsoft.com/v1.0/me/events", &json, "application/json", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

println!("Response status code = {}", resp.status_code());

let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load(&resp.body_str());
j_resp.set_emit_compact(false);
println!("{}", j_resp.emit().unwrap_or_default());

// The send succeeded if the response status code = 201.
if resp.status_code() != 201 {
    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

let odata_context = j_resp.string_of("\"@odata.context\"").unwrap_or_default();
let odata_etag = j_resp.string_of("\"@odata.etag\"").unwrap_or_default();
let id = j_resp.string_of("id").unwrap_or_default();
let created_date_time = j_resp.string_of("createdDateTime").unwrap_or_default();
let last_modified_date_time = j_resp.string_of("lastModifiedDateTime").unwrap_or_default();
let change_key = j_resp.string_of("changeKey").unwrap_or_default();
let transaction_id = j_resp.string_of("transactionId").unwrap_or_default();
let original_start_time_zone = j_resp.string_of("originalStartTimeZone").unwrap_or_default();
let original_end_time_zone = j_resp.string_of("originalEndTimeZone").unwrap_or_default();
let i_cal_u_id = j_resp.string_of("iCalUId").unwrap_or_default();
let reminder_minutes_before_start = j_resp.int_of("reminderMinutesBeforeStart");
let is_reminder_on = j_resp.bool_of("isReminderOn");
let has_attachments = j_resp.bool_of("hasAttachments");
let subject = j_resp.string_of("subject").unwrap_or_default();
let body_preview = j_resp.string_of("bodyPreview").unwrap_or_default();
let importance = j_resp.string_of("importance").unwrap_or_default();
let sensitivity = j_resp.string_of("sensitivity").unwrap_or_default();
let is_all_day = j_resp.bool_of("isAllDay");
let is_cancelled = j_resp.bool_of("isCancelled");
let is_organizer = j_resp.bool_of("isOrganizer");
let response_requested = j_resp.bool_of("responseRequested");
let series_master_id = j_resp.string_of("seriesMasterId").unwrap_or_default();
let show_as = j_resp.string_of("showAs").unwrap_or_default();
let mut v_type = j_resp.string_of("type").unwrap_or_default();
let web_link = j_resp.string_of("webLink").unwrap_or_default();
let online_meeting_url = j_resp.string_of("onlineMeetingUrl").unwrap_or_default();
let is_online_meeting = j_resp.bool_of("isOnlineMeeting");
let online_meeting_provider = j_resp.string_of("onlineMeetingProvider").unwrap_or_default();
let allow_new_time_proposals = j_resp.bool_of("allowNewTimeProposals");
let is_draft = j_resp.bool_of("isDraft");
let hide_attendees = j_resp.bool_of("hideAttendees");
let recurrence = j_resp.string_of("recurrence").unwrap_or_default();
let online_meeting = j_resp.string_of("onlineMeeting").unwrap_or_default();
let response_status_response = j_resp.string_of("responseStatus.response").unwrap_or_default();
let response_status_time = j_resp.string_of("responseStatus.time").unwrap_or_default();
let body_content_type = j_resp.string_of("body.contentType").unwrap_or_default();
let body_content = j_resp.string_of("body.content").unwrap_or_default();
let start_date_time = j_resp.string_of("start.dateTime").unwrap_or_default();
let start_time_zone = j_resp.string_of("start.timeZone").unwrap_or_default();
let end_date_time = j_resp.string_of("end.dateTime").unwrap_or_default();
let end_time_zone = j_resp.string_of("end.timeZone").unwrap_or_default();
let location_display_name = j_resp.string_of("location.displayName").unwrap_or_default();
let location_location_type = j_resp.string_of("location.locationType").unwrap_or_default();
let location_unique_id = j_resp.string_of("location.uniqueId").unwrap_or_default();
let location_unique_id_type = j_resp.string_of("location.uniqueIdType").unwrap_or_default();
let organizer_email_address_name = j_resp.string_of("organizer.emailAddress.name").unwrap_or_default();
let organizer_email_address_address = j_resp.string_of("organizer.emailAddress.address").unwrap_or_default();
let mut i = 0;
let mut count_i = j_resp.size_of_array("categories");
while i < count_i {
    j_resp.set_i(i);
    i = i + 1;
}

i = 0;
count_i = j_resp.size_of_array("locations");
while i < count_i {
    j_resp.set_i(i);
    let _ = j_resp.string_of("locations[i].displayName").unwrap_or_default();
    let _ = j_resp.string_of("locations[i].locationType").unwrap_or_default();
    let _ = j_resp.string_of("locations[i].uniqueId").unwrap_or_default();
    let _ = j_resp.string_of("locations[i].uniqueIdType").unwrap_or_default();
    i = i + 1;
}

i = 0;
count_i = j_resp.size_of_array("attendees");
while i < count_i {
    j_resp.set_i(i);
    v_type = j_resp.string_of("attendees[i].type").unwrap_or_default();
    let _ = j_resp.string_of("attendees[i].status.response").unwrap_or_default();
    let _ = j_resp.string_of("attendees[i].status.time").unwrap_or_default();
    let _ = j_resp.string_of("attendees[i].emailAddress.name").unwrap_or_default();
    let _ = j_resp.string_of("attendees[i].emailAddress.address").unwrap_or_default();
    i = i + 1;
}

println!("Event created.");