public async Task<ActionResult> Create(MyEvent myEvent) { if (Request.HttpMethod == "POST") { MyEventsRepository repo = new MyEventsRepository(); await repo.AddEvent(myEvent); return Redirect("/Events"); } else { myEvent.Start = DateTimeOffset.Now; myEvent.End = DateTimeOffset.Now.AddDays(1); return View(myEvent); } }
public async Task<MyEvent> GetEvent(string id) { var accessToken = await GetGraphAccessTokenAsync(); var restURL = string.Format("{0}me/events/{1}", SettingsHelper.GraphResourceUrl, id); var ev = new MyEvent(); try { using (HttpClient client = new HttpClient()) { var accept = "application/json"; client.DefaultRequestHeaders.Add("Accept", accept); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); using (var response = await client.GetAsync(restURL)) { if (response.IsSuccessStatusCode) { var item = JObject.Parse(await response.Content.ReadAsStringAsync()); if (item != null) { ev.Start = !string.IsNullOrEmpty(item["start"]["dateTime"].ToString()) ? DateTime.Parse(item["start"]["dateTime"].ToString()) : new DateTime(); ev.End = !string.IsNullOrEmpty(item["end"]["dateTime"].ToString()) ? DateTime.Parse(item["end"]["dateTime"].ToString()) : new DateTime(); ev.Id = !string.IsNullOrEmpty(item["id"].ToString()) ? item["id"].ToString() : string.Empty; ev.Subject = !string.IsNullOrEmpty(item["subject"].ToString()) ? item["subject"].ToString() : string.Empty; ev.Body = !string.IsNullOrEmpty(item["body"].ToString()) ? item["body"]["content"].ToString() : string.Empty; ev.Location = !string.IsNullOrEmpty(item["location"].ToString()) ? item["location"]["displayName"].ToString() : string.Empty; } } } } } catch (Exception el) { el.ToString(); } return ev; }
public async Task AddEvent(MyEvent myEvent) { var accessToken = await GetGraphAccessTokenAsync(); var restURL = string.Format("{0}me/events", SettingsHelper.GraphResourceUrl); try { using (HttpClient client = new HttpClient()) { var accept = "application/json"; client.DefaultRequestHeaders.Add("Accept", accept); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var ev = new Event { Subject = myEvent.Subject, Start = new Start { dateTime = myEvent.Start.ToString() }, End = new End { dateTime = myEvent.End.ToString() }, Location = new Location { displayName = myEvent.Location } }; string postBody = JsonConvert.SerializeObject(ev); using (var response = await client.PostAsync(restURL, new StringContent(postBody, Encoding.UTF8, "application/json"))) { if (response.IsSuccessStatusCode) return; else throw new Exception("add event error: " + response.StatusCode); } } } catch (Exception el) { el.ToString(); } }