private async Task <MeetingTimeSuggestionsResult> FindMeetingTimesAsync(IEnumerable <AttendeeBase> attendees, TimeConstraint timeConstraint = null, bool isOrgnizerOptional = true)
        {
            try
            {
                var suggestion = await _graphClient.Me.FindMeetingTimes(attendees, timeConstraint : timeConstraint, isOrganizerOptional : isOrgnizerOptional).Request().PostAsync();

                return(suggestion);
            }
            catch (ServiceException ex)
            {
                throw GraphClient.HandleGraphAPIException(ex);
            }
        }
        // Check the availability of people/rooms with corresponding emails on condition of startTime and duration.
        public async Task <List <bool> > CheckAvailableAsync(List <string> users, DateTime startTime, int availabilityViewInterval)
        {
            try
            {
                List <bool> availability = new List <bool>();
                var         schedules    = users;

                var intervalStartTime = new DateTimeTimeZone
                {
                    DateTime = startTime.ToString(),
                    TimeZone = "UTC"
                };

                var intervalEndTime = new DateTimeTimeZone
                {
                    DateTime = startTime.AddDays(1).ToString(),
                    TimeZone = "UTC"
                };

                ICalendarGetScheduleCollectionPage collectionPage = await _graphClient.Me.Calendar
                                                                    .GetSchedule(schedules, intervalEndTime, intervalStartTime, availabilityViewInterval)
                                                                    .Request()
                                                                    .PostAsync();

                // AvailabilityView[0] == '0' means available, while others mean not available.
                foreach (var page in collectionPage)
                {
                    // Can't find this room.
                    if (page.AvailabilityView == null)
                    {
                        availability.Add(false);
                        continue;
                    }

                    // AvailabilityViem is empty, should not get into this state.
                    if (page.AvailabilityView.Length == 0)
                    {
                        throw new Exception("There is no elements in AvailabilityView");
                    }

                    availability.Add(page.AvailabilityView.Length > 0 && page.AvailabilityView[0] == '0');
                }

                return(availability);
            }
            catch (ServiceException ex)
            {
                throw GraphClient.HandleGraphAPIException(ex);
            }
        }
        private async Task <Event> CreateEventAsync(Event newEvent)
        {
            try
            {
                // Add the event.
                var createdEvent = await _graphClient.Me.Events.Request().AddAsync(newEvent);

                return(createdEvent);
            }
            catch (ServiceException ex)
            {
                throw GraphClient.HandleGraphAPIException(ex);
            }
        }
        /// <summary>
        /// GetContactAsync.
        /// </summary>
        /// <param name="name">name.</param>
        /// <returns>Task contains List of Contacts.</returns>
        private async Task <List <Contact> > GetMSContactsAsync(string name)
        {
            List <Contact> items = new List <Contact>();

            var optionList   = new List <QueryOption>();
            var filterString = $"startswith(displayName, '{name}') or startswith(givenName,'{name}') or startswith(surname,'{name}')";

            optionList.Add(new QueryOption("$filter", filterString));

            // Get the current user's profile.
            IUserContactsCollectionPage contacts = null;

            try
            {
                contacts = await this._graphClient.Me.Contacts.Request(optionList).GetAsync();
            }
            catch (ServiceException ex)
            {
                throw GraphClient.HandleGraphAPIException(ex);
            }

            if (contacts?.Count > 0)
            {
                foreach (Contact contact in contacts)
                {
                    // Filter out conference rooms.
                    string displayName = contact.DisplayName ?? string.Empty;
                    if (!displayName.StartsWith("Conf Room"))
                    {
                        // Get user properties.
                        items.Add(contact);
                    }

                    if (items.Count >= 10)
                    {
                        break;
                    }
                }
            }

            return(items);
        }
        /// <summary>
        /// GetUsersAsync.
        /// </summary>
        /// <param name="name">name.</param>
        /// <returns>Task contains List of Users.</returns>
        private async Task <List <User> > GetMSUserAsync(string name)
        {
            var items        = new List <User>();
            var optionList   = new List <QueryOption>();
            var filterString = $"startswith(displayName, '{name}') or startswith(givenName,'{name}') or startswith(surname,'{name}') or startswith(mail,'{name}') or startswith(userPrincipalName,'{name}')";

            optionList.Add(new QueryOption("$filter", filterString));

            // Get the current user's profile.
            IGraphServiceUsersCollectionPage users = null;

            try
            {
                users = await _graphClient.Users.Request(optionList).GetAsync();
            }
            catch (ServiceException ex)
            {
                throw GraphClient.HandleGraphAPIException(ex);
            }

            if (users?.Count > 0)
            {
                foreach (var user in users)
                {
                    // Filter out conference rooms.
                    var displayName = user.DisplayName ?? string.Empty;
                    if (!displayName.StartsWith("Conf Room"))
                    {
                        // Get user properties.
                        items.Add(user);
                    }

                    if (items.Count >= 10)
                    {
                        break;
                    }
                }
            }

            return(items);
        }
        private async Task <List <Event> > GetMyCalendarViewByTimeAsync(DateTime startTime, DateTime endTime)
        {
            var items = new List <Event>();

            // Define the time span for the calendar view.
            var options = new List <QueryOption>
            {
                new QueryOption("startDateTime", startTime.ToString("o")),
                new QueryOption("endDateTime", endTime.ToString("o")),
                new QueryOption("$orderBy", "start/dateTime"),
            };

            IUserCalendarViewCollectionPage events = null;

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

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

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

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

            return(items);
        }
        // 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);
        }
        // Get events in all the current user's mail folders.
        private async Task <List <Event> > GetMyStartTimeEventsAsync(DateTime startTime)
        {
            var items = new List <Event>();

            // Get events.
            var options = new List <QueryOption>();
            var endTime = startTime.AddDays(3);

            options.Add(new QueryOption("startdatetime", startTime.ToString("o")));
            options.Add(new QueryOption("enddatetime", endTime.ToString("o")));
            options.Add(new QueryOption("$orderBy", "start/dateTime"));

            IUserCalendarViewCollectionPage events = null;

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

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

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

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

            return(items);
        }
        public async Task <PersonModel> GetMeAsync()
        {
            try
            {
                var me = await _graphClient.Me.Request().GetAsync();

                if (me != null)
                {
                    var url = await GetMSUserPhotoUrlAsyc(me.Id);

                    var personMe = new PersonModel(me.ToPerson());
                    personMe.Photo = url;

                    return(personMe);
                }

                return(null);
            }
            catch (ServiceException ex)
            {
                throw GraphClient.HandleGraphAPIException(ex);
            }
        }
        /// <summary>
        /// Get people whose name contains specified word.
        /// </summary>
        /// <param name="name">person name.</param>
        /// <returns>the persons list.</returns>
        private async Task <List <Person> > GetMSPeopleAsync(string name)
        {
            var items        = new List <Person>();
            var optionList   = new List <QueryOption>();
            var filterString = $"\"{name}\"";

            optionList.Add(new QueryOption("$search", filterString));

            // Get the current user's profile.
            IUserPeopleCollectionPage users = null;

            try
            {
                users = await _graphClient.Me.People.Request(optionList).GetAsync();
            }
            catch (ServiceException ex)
            {
                throw GraphClient.HandleGraphAPIException(ex);
            }

            // var users = await _graphClient.Users.Request(optionList).GetAsync();
            if (users?.Count > 0)
            {
                foreach (var user in users)
                {
                    // Filter out conference rooms.
                    var displayName = user.DisplayName ?? string.Empty;
                    if (!displayName.StartsWith("Conf Room"))
                    {
                        // Get user properties.
                        items.Add(user);
                    }
                }
            }

            return(items);
        }