Objective-C
Objective-C
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 Objective-C Downloads
#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <CkoCrypt2.h>
#import <CkoHttpResponse.h>
#import <NSString.h>
BOOL success = NO;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoHttp *http = [[CkoHttp alloc] init];
// Use your previously obtained access token here: Get Outlook Calendar OAuth2 Access Token (Azure AD v2.0 Endpoint).
CkoJsonObject *jsonToken = [[CkoJsonObject alloc] init];
success = [jsonToken LoadFile: @"qa_data/tokens/outlookCalendar.json"];
if (success == NO) {
NSLog(@"%@",jsonToken.LastErrorText);
return;
}
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.
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"subject" value: @"Let's go for lunch"];
[json UpdateString: @"body.contentType" value: @"HTML"];
[json UpdateString: @"body.content" value: @"Does noon work for you?"];
[json UpdateString: @"start.dateTime" value: @"2021-05-15T12:00:00"];
[json UpdateString: @"start.timeZone" value: @"Pacific Standard Time"];
[json UpdateString: @"end.dateTime" value: @"2021-05-15T14:00:00"];
[json UpdateString: @"end.timeZone" value: @"Pacific Standard Time"];
[json UpdateString: @"location.displayName" value: @"Harry's Bar"];
[json UpdateString: @"attendees[0].emailAddress.address" value: @"samanthab@contoso.onmicrosoft.com"];
[json UpdateString: @"attendees[0].emailAddress.name" value: @"Samantha Booth"];
[json UpdateString: @"attendees[0].type" value: @"required"];
[json UpdateBool: @"allowNewTimeProposals" value: YES];
// Generate a UUID.
CkoCrypt2 *crypt = [[CkoCrypt2 alloc] init];
[json UpdateString: @"transactionId" value: [crypt GenerateUuid]];
// Add the "Prefer" request header.
[http SetRequestHeader: @"Prefer" value: @"outlook.timezone=\"Pacific Standard Time\""];
// Send the HTTP POST
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpJson: @"POST" url: @"https://graph.microsoft.com/v1.0/me/events" json: json contentType: @"application/json" response: resp];
if (success == NO) {
NSLog(@"%@",http.LastErrorText);
return;
}
NSLog(@"%@%d",@"Response status code = ",[resp.StatusCode intValue]);
CkoJsonObject *jResp = [[CkoJsonObject alloc] init];
[jResp Load: resp.BodyStr];
jResp.EmitCompact = NO;
NSLog(@"%@",[jResp Emit]);
// The send succeeded if the response status code = 201.
if ([resp.StatusCode intValue] != 201) {
NSLog(@"%@",@"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
NSString *displayName = 0;
NSString *locationType = 0;
NSString *uniqueId = 0;
NSString *uniqueIdType = 0;
NSString *statusResponse = 0;
NSString *statusTime = 0;
NSString *emailAddressName = 0;
NSString *emailAddressAddress = 0;
NSString *odata_context = [jResp StringOf: @"\"@odata.context\""];
NSString *odata_etag = [jResp StringOf: @"\"@odata.etag\""];
NSString *id = [jResp StringOf: @"id"];
NSString *createdDateTime = [jResp StringOf: @"createdDateTime"];
NSString *lastModifiedDateTime = [jResp StringOf: @"lastModifiedDateTime"];
NSString *changeKey = [jResp StringOf: @"changeKey"];
NSString *transactionId = [jResp StringOf: @"transactionId"];
NSString *originalStartTimeZone = [jResp StringOf: @"originalStartTimeZone"];
NSString *originalEndTimeZone = [jResp StringOf: @"originalEndTimeZone"];
NSString *iCalUId = [jResp StringOf: @"iCalUId"];
int reminderMinutesBeforeStart = [[jResp IntOf: @"reminderMinutesBeforeStart"] intValue];
BOOL isReminderOn = [jResp BoolOf: @"isReminderOn"];
BOOL hasAttachments = [jResp BoolOf: @"hasAttachments"];
NSString *subject = [jResp StringOf: @"subject"];
NSString *bodyPreview = [jResp StringOf: @"bodyPreview"];
NSString *importance = [jResp StringOf: @"importance"];
NSString *sensitivity = [jResp StringOf: @"sensitivity"];
BOOL isAllDay = [jResp BoolOf: @"isAllDay"];
BOOL isCancelled = [jResp BoolOf: @"isCancelled"];
BOOL isOrganizer = [jResp BoolOf: @"isOrganizer"];
BOOL responseRequested = [jResp BoolOf: @"responseRequested"];
NSString *seriesMasterId = [jResp StringOf: @"seriesMasterId"];
NSString *showAs = [jResp StringOf: @"showAs"];
NSString *v_type = [jResp StringOf: @"type"];
NSString *webLink = [jResp StringOf: @"webLink"];
NSString *onlineMeetingUrl = [jResp StringOf: @"onlineMeetingUrl"];
BOOL isOnlineMeeting = [jResp BoolOf: @"isOnlineMeeting"];
NSString *onlineMeetingProvider = [jResp StringOf: @"onlineMeetingProvider"];
BOOL allowNewTimeProposals = [jResp BoolOf: @"allowNewTimeProposals"];
BOOL isDraft = [jResp BoolOf: @"isDraft"];
BOOL hideAttendees = [jResp BoolOf: @"hideAttendees"];
NSString *recurrence = [jResp StringOf: @"recurrence"];
NSString *onlineMeeting = [jResp StringOf: @"onlineMeeting"];
NSString *responseStatusResponse = [jResp StringOf: @"responseStatus.response"];
NSString *responseStatusTime = [jResp StringOf: @"responseStatus.time"];
NSString *bodyContentType = [jResp StringOf: @"body.contentType"];
NSString *bodyContent = [jResp StringOf: @"body.content"];
NSString *startDateTime = [jResp StringOf: @"start.dateTime"];
NSString *startTimeZone = [jResp StringOf: @"start.timeZone"];
NSString *endDateTime = [jResp StringOf: @"end.dateTime"];
NSString *endTimeZone = [jResp StringOf: @"end.timeZone"];
NSString *locationDisplayName = [jResp StringOf: @"location.displayName"];
NSString *locationLocationType = [jResp StringOf: @"location.locationType"];
NSString *locationUniqueId = [jResp StringOf: @"location.uniqueId"];
NSString *locationUniqueIdType = [jResp StringOf: @"location.uniqueIdType"];
NSString *organizerEmailAddressName = [jResp StringOf: @"organizer.emailAddress.name"];
NSString *organizerEmailAddressAddress = [jResp StringOf: @"organizer.emailAddress.address"];
int i = 0;
int count_i = [[jResp SizeOfArray: @"categories"] intValue];
while (i < count_i) {
jResp.I = [NSNumber numberWithInt: i];
i = i + 1;
}
i = 0;
count_i = [[jResp SizeOfArray: @"locations"] intValue];
while (i < count_i) {
jResp.I = [NSNumber numberWithInt: 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"] intValue];
while (i < count_i) {
jResp.I = [NSNumber numberWithInt: 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;
}
NSLog(@"%@",@"Event created.");