public async Task<HttpResponseMessage> PostOffice365Api(ApiCalendarModels apiCalendar) { if (ModelState.IsValid) { await apiCalendarServices.Create(apiCalendar); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, apiCalendar); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = apiCalendar.Id })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/<controller>/5 public async Task<HttpResponseMessage> Put(ApiCalendarModels apiCalendar) { if (ModelState.IsValid) { await apiCalendarServices.Update(apiCalendar); //Update var calendar = CalendarService.Get().Where(c => c.ApiId == apiCalendar.Id).FirstOrDefault(); calendar.Attendee = apiCalendar.Attendee; calendar.Body = apiCalendar.Body; calendar.Subject = apiCalendar.Subject; calendar.Date = apiCalendar.Date.HasValue ? apiCalendar.Date.Value.DateTime : (Nullable<DateTime>)null; CalendarService.Update(calendar); return Request.CreateResponse(HttpStatusCode.OK); } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); }
public IEnumerable<ApiCalendarModels> Get() { try { // Initialize values for the start and end times, and the number of appointments to retrieve. DateTime startDate = DateTime.Now; DateTime endDate = startDate.AddDays(30); const int NUM_APPTS = 1000; // Initialize the calendar folder object with only the folder ID. ExchangeService service = new ExchangeService(); #region Authentication // Set specific credentials. service.Credentials = new NetworkCredential("*****@*****.**", "HuyHung0912"); #endregion #region Endpoint management // Look up the user's EWS endpoint by using Autodiscover. service.AutodiscoverUrl("*****@*****.**", RedirectionCallback); Console.WriteLine("EWS Endpoint: {0}", service.Url); #endregion CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet()); // Set the start and end time and number of appointments to retrieve. CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS); // Limit the properties returned to the appointment's subject, start time, and end time. cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End); // Retrieve a collection of appointments by using the calendar view. FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView); var res = service.LoadPropertiesForItems( from Microsoft.Exchange.WebServices.Data.Item item in appointments select item, PropertySet.FirstClassProperties); var result = res.ToList(); List<ApiCalendarModels> data = new List<ApiCalendarModels>(); foreach (var item in result) { var itemResponse = (GetItemResponse)item; var appt = (Appointment)itemResponse.Item; var model = new ApiCalendarModels() { Id = itemResponse.Item.Id.UniqueId, Subject = itemResponse.Item.Subject, Date = appt.Start, End = appt.End, Body = itemResponse.Item.Body == null ? string.Empty : itemResponse.Item.Body.Text, Attendee = itemResponse.Item.DisplayTo == null ? string.Empty : itemResponse.Item.DisplayTo }; data.Add(model); } return data; } catch (Exception ex) { string s = ex.Message; } return null; }
public async Task<bool> Create(ApiCalendarModels model) { try { var _event = new Event() { Subject = model.Subject, End = model.End, Start = model.Date, Body = new ItemBody() { Content = model.Body } }; Microsoft.Office365.Exchange.Attendee attendee = new Microsoft.Office365.Exchange.Attendee(); attendee.Address = model.Attendee; _event.Attendees.Add(attendee); var client = await Office365Authenticator.GetClientInstance(); await client.Me.Events.AddEventAsync(_event); await client.Me.Events.ExecuteAsync(); return true; } catch (Exception ex) { } return false; }
public async Task<bool> Update(ApiCalendarModels model) { var client = await Office365Authenticator.GetClientInstance(); var _event = (await client.Me.Events.ExecuteAsync()).CurrentPage.Where(p => p.Id == model.Id).FirstOrDefault(); if (_event != null) { _event.Subject = model.Subject; _event.Start = model.Date; _event.End = model.End; await _event.UpdateAsync(); return true; } return false; }