lunedì 29 ottobre 2012

C# Google API - Retrieve All Calendar Feeds

The following code shows how to Retrieve All Calendar Feeds


public void GetCalendarFeeds()
        {
            //"_day" is a global Hashtable. The key is yearDay(string)-->the value is composed by
            // a list of object "Impegno". In this list there is an item for each events in day.
            //in the object "Impegno" there are the seguent info: start date, end date, title, description, event's uri.
            //I use this Hashtable to make update/delete of the events.
            //when i click on the MonthCalendar i load some variables with the references
            _days.Clear();

            EventQuery query = new EventQuery();
            CalendarService service = new CalendarService("appName");

            service.setUserCredentials("yourMail@gmail.com", "yourPassword");
            service.QueryClientLoginToken();

            //"cmb_calendars" is a ComboBox on the form containing the calendar.
            // view: C# Google API - Retrieve Own Calendars List
            query.Uri = new Uri("http://www.google.com/calendar/feeds/" + ((ComboboxItem)cmb_calendars.SelectedItem).Value.ToString() + "/private/full");

            //the start/end period i want to get
            query.StartTime = DateTime.Now.AddMonths(Convert.ToInt32("n-month before"));
            query.EndTime = DateTime.Now.AddMonths(Convert.ToInt32("n-month after"));

            EventFeed calFeed = service.Query(query) as EventFeed;
            DateTime[] events = new DateTime[calFeed.Entries.Count];

            while (calFeed != null && calFeed.Entries.Count > 0)
            {
                foreach (EventEntry entry in calFeed.Entries)
                {
                    When w = entry.Times[0];
                    DateTime d = w.StartTime;
                    string day = d.Year.ToString() + d.DayOfYear.ToString();


                    // if the feed is into _day, remove it and add it else...
                    if (_days.ContainsKey(day))
                    {
                        //the list of the events per day
                        List<Impegno> even = (List<Impegno>)_days[day];
                        //below the definition of the class "Impegno" 
                        Impegno imp = new Impegno(w.StartTime, w.EndTime, entry.Title.Text, "", entry.EditUri.ToString());
                        even.Add(imp);
                        _days.Remove(day);
                        _days.Add(day, even);
                    }
                    else
                    {
                        // if the feed is not present add it
                        List<Impegno> eventi = new List<Impegno>();
                        Impegno imp = new Impegno(w.StartTime, w.EndTime, entry.Title.Text, "", entry.EditUri.ToString());
                        eventi.Add(imp);
                        _days.Add(day, eventi);
                    }

                    //calendar is a MonthCalendar on the form
                    calendar.AddBoldedDate(d);
                }

                if (calFeed.NextChunk != null)
                {
                    query.Uri = new Uri(calFeed.NextChunk);
                    calFeed = service.Query(query) as EventFeed;
                }
                else
                    calFeed = null;
            }

            calendar.UpdateBoldedDates();
        }


*The following code shows the definition of the class "Impegno"


   class Impegno
    {
        public DateTime _start;
        public DateTime _end;
        public string _title;
        public string _description;
        public string _uri;

        public Impegno(DateTime start, DateTime end, string title, string description, string uri)
        {
            _start = start;
            _end = end;
            _title = title;
            _description = description;
            _uri = uri;
        }
    }


** I want to remeber to go here http://support.google.com/mail/bin/answer.py?hl=it&answer=1173270 to get  Application-specific password

4 commenti:

  1. Hi, your code works perfect, but how can load the events to a Data Grid?

    RispondiElimina
  2. Hi,
    thank you for using my code.
    To load the events to DataGrid you can refer to this: http://dotnetoday.blogspot.it/2013/02/binding-datagridview-with-hashtable.html

    In our example the code will be the seguent:

    public void GetCalendarFeeds()
    {
    _days.Clear();

    ………
    ………
    ………
    BindYourDataGridView();

    calendar.UpdateBoldedDates();
    }


    private void BindYourDataGridView()
    {
    DataSet ds = new DataSet();
    DataTable dt = ds.Tables.Add("data");

    dt.Columns.Add("A", typeof(DateTime));
    dt.Columns.Add("B", typeof(string));

    IDictionaryEnumerator enumerator = _days.GetEnumerator();

    DataRow row = null;

    //adding row in the datatable
    while (enumerator.MoveNext())
    {
    string index = (string)enumerator.Key;

    int year = Convert.ToInt32(index.Substring(0, 4));
    int day = Convert.ToInt32(index.Substring(4, (index.Length - 4)));
    DateTime theDate = new DateTime(year, 1, 1).AddDays(day - 1);

    string value = "";

    object imp = enumerator.Value;

    List limp = (List)imp;

    foreach (Impegno item in limp)
    {
    value += item._title + ";";
    }

    row = dt.NewRow();
    row["A"] = theDate;
    row["B"] = value;
    dt.Rows.Add(row);
    }

    dataGridView1.DataSource = ds.Tables[0];
    }

    RispondiElimina
  3. Hi, there is a way to retrieve event's photo?

    RispondiElimina
    Risposte
    1. hello, thank you to visit my blog! you mean attachment to the event?

      Elimina