public async Task<ActionResult> Create() {
      var myEvent = new MyEvent {
        Start = DateTimeOffset.Now,
        End = DateTimeOffset.Now.AddDays(1)
      };

      return View(myEvent);
    }
    public async Task AddEvent(MyEvent myEvent) {
      var client = await EnsureClientCreated();

      var myEventLocation = new Location {
        DisplayName = myEvent.Location
      };

      var newEvent = new Event {
        Subject = myEvent.Subject,
        Start = myEvent.Start,
        End = myEvent.End,
        Location = myEventLocation
      };
      await client.Me.Events.AddEventAsync(newEvent);
    }
        public async Task<List<MyEvent>> GetEvents(int pageIndex, int pageSize)
        {
            var client = await EnsureClientCreated();

            var eventsResults = await (from ev in client.Me.Events
                                       select ev)
                                       .Skip(pageIndex * pageSize)
                                       .Take(pageSize)
                                       .ExecuteAsync();

            var events = eventsResults.CurrentPage.OrderBy(e => e.Start);

            // indicate if more results available
            MorePagesAvailable = eventsResults.MorePagesAvailable;

            var eventList = new List<MyEvent>();
            foreach (var myEvent in events)
            {
                var newEvent = new MyEvent
                {
                    Id = myEvent.Id,
                    Subject = myEvent.Subject,
                    Start = myEvent.Start,
                    End = myEvent.End
                };
                if (myEvent.Body != null)
                {
                    newEvent.Body = myEvent.Body.Content;
                }
                if (myEvent.Location != null)
                {
                    newEvent.Location = myEvent.Location.DisplayName;
                }
                if (myEvent.Start != null)
                {
                    newEvent.Start = myEvent.Start;
                }

                eventList.Add(newEvent);
            }

            return eventList;

        }
    public async Task<MyEvent> GetEvent(string id) {
      var client = await EnsureClientCreated();
      var ev = await client.Me.Events.GetById(id).ExecuteAsync();

      var newEvent = new MyEvent {
        Id = ev.Id,
        Subject = ev.Subject,
        Start = ev.Start,
        End = ev.End
      };
      if (ev.Location != null) {
        newEvent.Location = ev.Location.DisplayName;
      }
      if (ev.Body != null) {
        newEvent.Body = ev.Body.Content;
      }

      return newEvent;
    }
Exemplo n.º 5
0
        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<MyEvent> GetEvent(string id)
        {
            try
            {
                var graphServiceClient = await GetGraphServiceAsync();
                var requestEvent = await graphServiceClient.Me.Events[id].Request().GetAsync();
                var eventResult = new MyEvent
                {
                    Id = requestEvent.Id,
                    Subject = requestEvent.Subject,
                    Body = requestEvent.Body.Content,
                    Location = requestEvent.Location.DisplayName,
                    Start = DateTime.SpecifyKind(DateTime.Parse(requestEvent.Start.DateTime), requestEvent.Start.TimeZone == "UTC" ? DateTimeKind.Utc : DateTimeKind.Local).ToLocalTime(),
                    End = DateTime.SpecifyKind(DateTime.Parse(requestEvent.End.DateTime), requestEvent.End.TimeZone == "UTC" ? DateTimeKind.Utc : DateTimeKind.Local).ToLocalTime(),
                };
                return eventResult;
            }
            catch
            {
                return null;
            }

        }
        public async Task <MyEvent> GetEvent(string id)
        {
            try
            {
                var graphServiceClient = await GetGraphServiceAsync();

                var requestEvent = await graphServiceClient.Me.Events[id].Request().GetAsync();
                var eventResult  = new MyEvent
                {
                    Id       = requestEvent.Id,
                    Subject  = requestEvent.Subject,
                    Body     = requestEvent.Body.Content,
                    Location = requestEvent.Location.DisplayName,
                    Start    = DateTime.SpecifyKind(DateTime.Parse(requestEvent.Start.DateTime), requestEvent.Start.TimeZone == "UTC" ? DateTimeKind.Utc : DateTimeKind.Local).ToLocalTime(),
                    End      = DateTime.SpecifyKind(DateTime.Parse(requestEvent.End.DateTime), requestEvent.End.TimeZone == "UTC" ? DateTimeKind.Utc : DateTimeKind.Local).ToLocalTime(),
                };
                return(eventResult);
            }
            catch
            {
                return(null);
            }
        }
        public async Task <MyEvent> GetEvent(string id)
        {
            var client = await EnsureClientCreated();

            var ev = await client.Me.Events.GetById(id).ExecuteAsync();

            var newEvent = new MyEvent {
                Id      = ev.Id,
                Subject = ev.Subject,
                Start   = ev.Start,
                End     = ev.End
            };

            if (ev.Location != null)
            {
                newEvent.Location = ev.Location.DisplayName;
            }
            if (ev.Body != null)
            {
                newEvent.Body = ev.Body.Content;
            }

            return(newEvent);
        }
    public async Task<ActionResult> Create(MyEvent myEvent) {

      await _repo.AddEvent(myEvent);
      return Redirect("/Events");
    }
 public async Task AddEvent(MyEvent myEvent)
 {
     try
     {
         var graphServiceClient = await GetGraphServiceAsync();
         var requestEvent = new Microsoft.Graph.Event
         {
             Subject = myEvent.Subject,
             Start = new DateTimeTimeZone() { DateTime = myEvent.Start.ToString(), TimeZone = DateTimeKind.Local.ToString() },
             End = new DateTimeTimeZone { DateTime = myEvent.End.ToString(), TimeZone = DateTimeKind.Local.ToString() },
             Location = new Microsoft.Graph.Location { DisplayName = myEvent.Location },
             Body = new ItemBody { Content = myEvent.Body }
         };
         await graphServiceClient.Me.Events.Request().AddAsync(requestEvent);
     }
     catch
     {
     }
     return;
 }
Exemplo n.º 11
0
        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 },
                        Body = new Body { content = myEvent.Body }
                    };
                    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();
            }
        }
        public async Task<List<MyEvent>> Search(string searchTerm)
        {
            var client = await EnsureClientCreated();

            var eventsResults = await (from ev in client.Me.Events
                                       where ev.Subject.Contains(searchTerm)
                                       select ev)
              .ExecuteAsync();

            var events = eventsResults.CurrentPage.OrderBy(e => e.Start);

            var eventList = new List<MyEvent>();
            foreach (var myEvent in events)
            {
                var newEvent = new MyEvent
                {
                    Id = myEvent.Id,
                    Subject = myEvent.Subject,
                    Start = myEvent.Start,
                    End = myEvent.End
                };
                if (myEvent.Body != null)
                {
                    newEvent.Body = myEvent.Body.Content;
                }
                if (myEvent.Location != null)
                {
                    newEvent.Location = myEvent.Location.DisplayName;
                }
                if (myEvent.Start != null)
                {
                    newEvent.Start = myEvent.Start;
                }

                eventList.Add(newEvent);
            }

            return eventList;
        }