0

I am trying to query free busy data from Google calendar. Simply I am providing start date/time and end date/time. All I want to know is if this time frame is available or not. When I run below query, I get "responseOBJ" response object which doesn't seem to include what I need. The response object only contains start and end time. It doesn't contain flag such as "IsBusy" "IsAvailable"

https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query

        #region Free_busy_request_NOT_WORKING
        FreeBusyRequest requestobj = new FreeBusyRequest();

        FreeBusyRequestItem c = new FreeBusyRequestItem();
        c.Id = "calendarresource@domain.com";
        requestobj.Items = new List<FreeBusyRequestItem>();
        requestobj.Items.Add(c);

        requestobj.TimeMin = DateTime.Now.AddDays(1);
        requestobj.TimeMax = DateTime.Now.AddDays(2);


        FreebusyResource.QueryRequest TestRequest = calendarService.Freebusy.Query(requestobj);
      //  var TestRequest = calendarService.Freebusy.
       // FreeBusyResponse responseOBJ = TestRequest.Execute();
        var responseOBJ = TestRequest.Execute();
        #endregion
peleyal
  • 3,472
  • 1
  • 14
  • 25
ZOO
  • 15
  • 7

2 Answers2

2

Calendar API will only ever provide ordered busy blocks in the response, never available blocks. Everything outside busy is available. Do you have at least one event on the calendar with the given ID in the time window? Also the account you are using needs to have at least free-busy access to the resource to be able to retrieve availability.

lucka
  • 21
  • 1
  • Thanks Lucka. Does this mean if I get null from responseOBJ , requested time frame is avalible ? – ZOO Apr 14 '14 at 21:30
0

I know this question is old, however I think it would be beneficial to see an example. You will needed to actually grab the Busy information from your response. Below is a snippet from my own code (minus the call) with how to handle the response. You will need to utilized your c.Id as the key to search through the response:

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

bool checkBusy;
bool containsKey;
if (responseObject.Calendars.ContainsKey("**INSERT YOUR KEY HERE**"))
{
    containsKey = true;
    if (containsKey)
    {
        //Had to deconstruct API response by WriteLine(). Busy returns a count of 1, while being free returns a count of 0. 
        //These are properties of a dictionary and a List of the responseObject (dictionary returned by API POST).
        if (responseObject.Calendars["**YOUR KEY HERE**"].Busy.Count == 0)
        {
            checkBusy = false;
            //WriteLine(checkBusy);
        }
        else
        {
            checkBusy = true;
            //WriteLine(checkBusy);
        }

        if (checkBusy == true)
        {
            var busyStart = responseObject.Calendars["**YOUR KEY HERE**"].Busy[0].Start;
            var busyEnd = responseObject.Calendars["**YOUR KEY HERE**].Busy[0].End;
            //WriteLine(busyStart);
            //WriteLine(busyEnd);
            //Read();

            string isBusyString = "Between " + busyStart + " and " + busyEnd + " your trainer is busy";
            richTextBox1.Text = isBusyString;
        }
        else
        {
            string isFreeString = "Between " + startDate + " and " + endDate + " your trainer is free";
            richTextBox1.Text += isFreeString;
        }
    }
    else
    {
        richTextBox1.Clear();
        MessageBox.Show("CalendarAPIv3 has failed, please contact support\nregarding missing <key>", "ERROR!");
    }
}

My suggestion would be to break your responses down by writing them to the console. Then, you can "deconstruct" them. That is how I was able to figure out "where" to look within the response. As noted above, you will only receive the information for busyBlocks. I used the date and time that was selected by my client's search to show the "free" times.

EDIT:

You'll need to check if your key exists before attempting the TryGetValue or searching with a keyvaluepair.

tehhowch
  • 9,645
  • 4
  • 24
  • 42