// Get a specified event.
        public async Task <EventsItem> GetEvent(GraphServiceClient graphClient, string id)
        {
            EventsItem items = new EventsItem();

            // Get the event.
            Event retrievedEvent = await graphClient.Me.Events[id].Request().GetAsync();

            if (retrievedEvent != null)
            {
                items = new EventsItem()
                {
                    Id         = retrievedEvent.Id,
                    Body       = retrievedEvent.Body,
                    Attendees  = retrievedEvent.Attendees.ToList(),
                    Location   = retrievedEvent.Location,
                    EndTime    = retrievedEvent.End,
                    StartTime  = retrievedEvent.Start,
                    Subject    = retrievedEvent.Subject,
                    EventDate  = DateTime.Parse(retrievedEvent.Start.DateTime.ToString()).Date,
                    EventDay   = DateTime.Parse(retrievedEvent.Start.DateTime.ToString()).DayOfWeek,
                    EventStart = DateTime.Parse(retrievedEvent.Start.DateTime.ToString()).ToString("t"),
                    EventEnd   = DateTime.Parse(retrievedEvent.End.DateTime.ToString()).ToString("t"),
                };
            }
            return(items);
        }
        // Delete a specified event.
        public async Task <EventsItem> DeleteEvent(GraphServiceClient graphClient, string id)
        {
            EventsItem items = new EventsItem();

            // Delete the event.
            await graphClient.Me.Events[id].Request().DeleteAsync();

            return(items);
        }
        // Update an event.
        // This snippets updates the event subject, time, and attendees.
        public async Task <EventsItem> UpdateEvent(GraphServiceClient graphClient, string id, EventsItem eventsItem)
        {
            EventsItem items = new EventsItem();

            // Get the current list of attendees, and then add an attendee.
            Event           originalEvent = await graphClient.Me.Events[id].Request().Select("attendees").GetAsync();
            List <Attendee> attendees     = originalEvent.Attendees as List <Attendee>;

            // Update the event.
            Event updatedEvent = await graphClient.Me.Events[id].Request().UpdateAsync(new Event
            {
                Subject   = eventsItem.Subject,
                Attendees = attendees,
                Start     = eventsItem.StartTime,
                End       = eventsItem.EndTime,
                IsAllDay  = false
            });

            if (updatedEvent != null)
            {
                // Get updated event properties.
                items = new EventsItem()
                {
                    Id         = updatedEvent.Id,
                    Body       = updatedEvent.Body,
                    Attendees  = updatedEvent.Attendees.ToList(),
                    Location   = updatedEvent.Location,
                    EndTime    = updatedEvent.End,
                    StartTime  = updatedEvent.Start,
                    Subject    = updatedEvent.Subject,
                    EventDate  = DateTime.Parse(updatedEvent.Start.DateTime.ToString()).Date,
                    EventDay   = DateTime.Parse(updatedEvent.Start.DateTime.ToString()).DayOfWeek,
                    EventStart = DateTime.Parse(updatedEvent.Start.DateTime.ToString()).ToString("t"),
                    EventEnd   = DateTime.Parse(updatedEvent.End.DateTime.ToString()).ToString("t"),
                };
            }
            return(items);
        }
        //To create event for student
        public async Task <EventsItem> CreateStudentEvent(GraphServiceClient graphClient, EventsItem eventsItem)
        {
            EventsItem items = new EventsItem();


            // Add the event.
            Event createdEvent = await graphClient.Me.Events.Request().AddAsync(new Event
            {
                Subject   = eventsItem.Subject,
                Start     = eventsItem.StartTime,
                End       = eventsItem.EndTime,
                Attendees = eventsItem.Attendees,
                Location  = eventsItem.Location,
                IsAllDay  = false
            });

            if (createdEvent != null)
            {
                // Get updated event properties.
                items = new EventsItem()
                {
                    Id         = createdEvent.Id,
                    Body       = createdEvent.Body,
                    Attendees  = createdEvent.Attendees.ToList(),
                    Location   = createdEvent.Location,
                    EndTime    = createdEvent.End,
                    StartTime  = createdEvent.Start,
                    Subject    = createdEvent.Subject,
                    EventDate  = DateTime.Parse(createdEvent.Start.DateTime.ToString()).Date,
                    EventDay   = DateTime.Parse(createdEvent.Start.DateTime.ToString()).DayOfWeek,
                    EventStart = DateTime.Parse(createdEvent.Start.DateTime.ToString()).ToString("t"),
                    EventEnd   = DateTime.Parse(createdEvent.End.DateTime.ToString()).ToString("t"),
                };
            }
            return(items);
        }
        // Create an event faculty.
        public async Task <EventsItem> CreateEvent(GraphServiceClient graphClient, EventsItem eventsItem)
        {
            EventsItem items = new EventsItem();
            Event      createdEvent;
            string     calendarId = null;

            //Get Calendar of office hours
            // Define the required calendar.
            List <QueryOption> options = new List <QueryOption>();

            options.Add(new QueryOption("filter", "startswith(name, 'officehours')"));
            //var calander = await graphClient.Me.Calendars.Request(options).GetAsync();
            var calander = await graphClient.Me.Calendars.Request().GetAsync();

            foreach (var cal in calander)
            {
                if (cal.Name.ToUpper().Equals("OFFICEHOURS"))
                {
                    calendarId = cal.Id;
                }
            }


            // Add the event.
            if (calander != null)
            {
                //Add event to specific calander
                createdEvent = await graphClient.Me.Calendars[calendarId].Events.Request().AddAsync(new Event
                {
                    Subject   = eventsItem.Subject,
                    Start     = eventsItem.StartTime,
                    End       = eventsItem.EndTime,
                    Attendees = eventsItem.Attendees,
                    Location  = eventsItem.Location,
                    IsAllDay  = false
                });
            }
            else
            {
                createdEvent = await graphClient.Me.Events.Request().AddAsync(new Event
                {
                    Subject   = eventsItem.Subject,
                    Start     = eventsItem.StartTime,
                    End       = eventsItem.EndTime,
                    Attendees = eventsItem.Attendees,
                    Location  = eventsItem.Location,
                    IsAllDay  = false
                });
            }


            if (createdEvent != null)
            {
                // Get updated event properties.
                items = new EventsItem()
                {
                    Id         = createdEvent.Id,
                    Body       = createdEvent.Body,
                    Attendees  = createdEvent.Attendees.ToList(),
                    Location   = createdEvent.Location,
                    EndTime    = createdEvent.End,
                    StartTime  = createdEvent.Start,
                    Subject    = createdEvent.Subject,
                    EventDate  = DateTime.Parse(createdEvent.Start.DateTime.ToString()).Date,
                    EventDay   = DateTime.Parse(createdEvent.Start.DateTime.ToString()).DayOfWeek,
                    EventStart = DateTime.Parse(createdEvent.Start.DateTime.ToString()).ToString("t"),
                    EventEnd   = DateTime.Parse(createdEvent.End.DateTime.ToString()).ToString("t"),
                };
            }
            return(items);
        }