예제 #1
0
        private async void GetBirthdaysButton_Click(Object sender, RoutedEventArgs e)
        {
            try
            {
                var options = new List <QueryOption>
                {
                    new QueryOption("startDateTime", DateTime.Now.ToString("o")),
                    new QueryOption("endDateTime", DateTime.Now.AddDays(14).ToString("o"))
                };
                calendar = await graphClient.Me.Calendar.CalendarView.Request(options)
                           .Filter("categories/any(categories: categories eq 'Birthday')")
                           .Select("subject,start,categories").GetAsync();

                MyEvents = new ObservableCollection <Models.Event>();

                foreach (var myEvent in calendar)
                {
                    MyEvents.Add(new Models.Event
                    {
                        Id      = myEvent.Id,
                        Subject = myEvent.Subject ?? "No subject",
                        Start   = DateTime.Parse(myEvent.Start.DateTime).ToLocalTime()
                    });
                }

                EventCountTextBlock.Text   = $"You have {MyEvents.Count()} birthdays in the next 2 weeks:";
                EventsDataGrid.ItemsSource = MyEvents;
            }
            catch (ServiceException ex)
            {
                EventCountTextBlock.Text = $"We could not get birthdays: {ex.Error.Message}";
            }
        }
        // Get user's calendar view.
        // This snippets gets events for the next seven days.
        public async Task <List <ResultsItem> > GetMyCalendarView(GraphServiceClient graphClient)
        {
            List <ResultsItem> items = new List <ResultsItem>();

            // Define the time span for the calendar view.
            List <QueryOption> options = new List <QueryOption>();

            options.Add(new QueryOption("startDateTime", DateTime.Now.ToString("o")));
            options.Add(new QueryOption("endDateTime", DateTime.Now.AddDays(7).ToString("o")));

            ICalendarCalendarViewCollectionPage calendar = await graphClient.Me.Calendar.CalendarView.Request(options).GetAsync();

            if (calendar?.Count > 0)
            {
                foreach (Event current in calendar)
                {
                    items.Add(new ResultsItem
                    {
                        Display = current.Subject,
                        Id      = current.Id
                    });
                }
            }
            return(items);
        }
예제 #3
0
        // Get calendars
        public async Task <List <ResultsItem> > GetCalendars(GraphServiceClient graphClient, string userID = null, string groupID = null)
        {
            List <ResultsItem> items = new List <ResultsItem>();

            // default: [email protected] TODO: ändern
            userID = userID ?? "f099d2f3-aab5-4ceb-a9e6-c45ad01a8212";
            // default: Mitarbeitergruppe in [email protected] TODO: ändern
            groupID = groupID ?? "AAMkAGRjM2JiYjUxLTdkMzQtNDBiNy1hYTE0LTY1NTg4YjNkMjhiMABGAAAAAABURYew-Y1vRoSHy1UQetQ6BwCzMNTQWwFmRoqNfNysC6efAAAAAAEGAACzMNTQWwFmRoqNfNysC6efAAAEAbSiAAA=";

            ICalendarGroupCalendarsCollectionPage calendars = await graphClient.Users[userID].CalendarGroups[groupID].Calendars.Request().GetAsync();

            foreach (Calendar cal in calendars)
            {
                items.Add(new ResultsItem
                {
                    Display = cal.Name,
                    Id      = cal.Id,
                    Type    = cal.GetType().Name
                });

                // Get Events of each calendar within the desired timerange
                List <QueryOption> options = new List <QueryOption>();
                options.Add(new QueryOption("StartDateTime", "2015-11-08T19:00:00.0000000"));
                options.Add(new QueryOption("EndDateTime", "2016-11-08T19:00:00.0000000"));

                ICalendarCalendarViewCollectionPage calendarview = await graphClient.Users[userID].CalendarGroups[groupID].Calendars[cal.Id].CalendarView.Request(options).Select("subject,start,end,showAs").GetAsync();
                foreach (Event ev in calendarview)
                {
                    items.Add(new ResultsItem
                    {
                        Display    = ev.Subject,
                        Id         = ev.Id,
                        Type       = ev.GetType().Name,
                        Properties = new Dictionary <string, object>
                        {
                            { "start", ev.Start.DateTime },
                            { "end", ev.End.DateTime }
                        }
                    });
                }
            }

            return(items);
        }
예제 #4
0
        private async void GetTodayEventsButton_Click(Object sender, RoutedEventArgs e)
        {
            try
            {
                var options = new List <QueryOption>
                {
                    new QueryOption("startDateTime", DateTime.Today.ToString("o")),
                    new QueryOption("endDateTime", DateTime.Today.AddDays(1).ToString("o"))
                };
                calendar = await graphClient.Me.Calendar.CalendarView.Request(options)
                           .Select("subject,organizer,location,start,end")
                           .GetAsync();

                MyEvents = new ObservableCollection <Models.Event>();

                foreach (var myEvent in calendar)
                {
                    MyEvents.Add(new Models.Event
                    {
                        Id      = myEvent.Id,
                        Subject = (myEvent.Subject != null) ?
                                  myEvent.Subject :
                                  "No subject",
                        Location = (myEvent.Location != null) ?
                                   myEvent.Location.DisplayName :
                                   "Unknown location",
                        Organizer = (myEvent.Organizer != null) ?
                                    myEvent.Organizer.EmailAddress.Name :
                                    "Unknown organizer",
                        Start = DateTime.Parse(myEvent.Start.DateTime).ToLocalTime(),
                        End   = DateTime.Parse(myEvent.End.DateTime).ToLocalTime()
                    });
                }

                EventCountTextBlock.Text   = $"You have {calendar.Count()} events today:";
                EventsDataGrid.ItemsSource = MyEvents;
            }
            catch (ServiceException ex)
            {
                EventCountTextBlock.Text = $"We could not get today's events: {ex.Error.Message}";
            }
        }
예제 #5
0
        public async Task <List <Event> > GetEventsBetweenDatesAsync(
            string calendarName, DateTimeZone start, DateTimeZone end, CancellationToken cancellationToken = default)
        {
            try
            {
                var calendarId = await GetCalendarIdAsync(calendarName, cancellationToken);

                var startDate = start.UniversalTime.Date.ToString("o");
                var endDate   = end.UniversalTime.Date.ToString("o");

                var queryOptions = new List <QueryOption>()
                {
                    new QueryOption("startDateTime", startDate),
                    new QueryOption("endDateTime", endDate)
                };

                ICalendarCalendarViewCollectionPage page = await _graphServiceClient.Me.Calendars[calendarId].CalendarView
                                                           .Request(queryOptions)
                                                           .GetAsync(cancellationToken);
                List <Event> events = page.ToList();

                // Query the rest of the pages
                while (page.NextPageRequest is not null)
                {
                    page = await page.NextPageRequest.GetAsync(cancellationToken);

                    events.AddRange(page.ToList());
                }

                _logger.LogInformation("{this} success, {eventCount} events found.",
                                       nameof(GetEventsBetweenDatesAsync),
                                       events.Count);

                return(events);
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex, "{this} failed.", nameof(GetEventsBetweenDatesAsync));
                return(new());
            }
        }
        // Get user's calendar view.
        // This snippets gets events for the next seven days.
        private async Task <List <Event> > GetMyUpcomingCalendarViewAsync(TimeSpan?timeSpan = null)
        {
            var items = new List <Event>();

            // Define the time span for the calendar view.
            var options = new List <QueryOption>
            {
                new QueryOption("startDateTime", DateTime.UtcNow.ToString("o")),
                new QueryOption("endDateTime", timeSpan == null ? DateTime.UtcNow.AddDays(1).ToString("o") : DateTime.UtcNow.Add(timeSpan.Value).ToString("o")),
                new QueryOption("$orderBy", "start/dateTime"),
            };

            ICalendarCalendarViewCollectionPage calendar = null;

            try
            {
                calendar = await _graphClient.Me.Calendar.CalendarView.Request(options).GetAsync();
            }
            catch (ServiceException ex)
            {
                throw GraphClient.HandleGraphAPIException(ex);
            }

            if (calendar?.Count > 0)
            {
                items.AddRange(calendar);
            }

            while (calendar.NextPageRequest != null)
            {
                calendar = await calendar.NextPageRequest.GetAsync();

                if (calendar?.Count > 0)
                {
                    items.AddRange(calendar);
                }
            }

            return(items);
        }