Example #1
0
        static FreeBusyResponse LoadRequest(string account)
        {
            FreeBusyRequestItem calendarRequested = new FreeBusyRequestItem();

            var today = DateTime.Now.ToString("yyyy-MM-dd");

            FreeBusyRequest requestBody = new FreeBusyRequest {
                TimeMin  = Convert.ToDateTime(today + "T00:00:00.000Z"),
                TimeMax  = Convert.ToDateTime(today + "T23:59:59.000Z"),
                Items    = new List <FreeBusyRequestItem>(),
                TimeZone = "Europe/Rome"
            };

            calendarRequested.Id = account;
            requestBody.Items.Add(calendarRequested);

            FreebusyResource.QueryRequest request = _service.Freebusy.Query(requestBody);
            return(request.Execute());
        }
Example #2
0
        public List <Appointment> FreeBusyCheck(DateTime startDate, DateTime endDate)
        {
            FreeBusyRequest request = new FreeBusyRequest();

            request.TimeMin = startDate;
            request.TimeMax = endDate;
            var items = new List <FreeBusyRequestItem>();
            var item  = new FreeBusyRequestItem();

            item.Id   = calendarId;
            item.ETag = "";
            items.Add(item);
            request.Items = items;
            var appointments = GetAppointments(startDate, endDate);

            FreebusyResource.QueryRequest testRequest = service.Freebusy.Query(request);
            var responseObject = testRequest.Execute();

            if (responseObject.Calendars.ContainsKey(calendarId))
            {
                if (responseObject.Calendars[calendarId].Busy.Count > 0)
                {
                    foreach (var busyItem in responseObject.Calendars[calendarId].Busy)
                    {
                        var busyStart = busyItem.Start;
                        var busyEnd   = busyItem.End;

                        // gets all the available date times between busy start and end date
                        var starts = appointments.Where(a => a.StartDate >= busyStart).ToList();
                        var ends   = starts.Where(a => a.StartDate < busyEnd).ToList();

                        foreach (var appointment in ends)
                        {
                            appointments[appointments.IndexOf(appointment)].IsBusy = true;
                        }
                    }
                }
            }
            return(appointments);
        }