C# .NET - to use google calendar - Asked By Prat Mandav on 03-Oct-11 02:12 AM

I need to use google calendar in my website

is there anyone who has used google calendar & can give some idea atleast
Web Star replied to Prat Mandav on 03-Oct-11 02:18 AM

What is Google Calendar?

Google calendar is a free Google service for planning of meetings, events, affairs with a binding to a calendar. It is possible to set a time for a meeting, repetition, a reminder, to invite other participants (service sends the invitation by e-mail). It is possible to create several calendars, each with its own name.

Possible Ways to Solve the Problem

Google has Calendar Data API that allows you to view and update calendar events from your application.

The following Calendar Data API libraries are there:

  1. http://code.google.com/apis/calendar/docs/1.0/developers_guide_dotnet.html
  2. http://code.google.com/apis/calendar/docs/1.0/developers_guide_java.html
  3. http://code.google.com/apis/calendar/docs/1.0/developers_guide_js.html
  4. http://code.google.com/apis/calendar/docs/1.0/developers_guide_php.html
  5. http://code.google.com/apis/calendar/docs/1.0/developers_guide_python.html

We will use .NET library for our task, because we have ASP.NET application where we need to work with Google Calendar service.

Meaningful Statement of the Problem

  1. Get an array of events from the Google Calendar, using the following information: "Google calendar name", "Google account name", "Google account password".
  2. Display these events on the site.

Solution of the Problem

(1) Get an array of events from the Google Calendar, using the following information: "Google calendar name", "Google account name", "Google account password".

Consider the solution to this problem step by step:

Step 1: Download .NET library for Google data API

You can download the .NET library for Google data API from http://code.google.com/p/google-gdata/downloads/detail?name=Google_Data_API_Setup_1.8.0.0.msi&can=2&q= Then, run the installer. The library will be installed into the following folder: C:\Program Files\Google\Google Data API SDK\Redist\.

Step 2: Copy the necessary files to the Web site

Copy the following files from C:\Program Files\Google\Google Data API SDK\Redist\ into Bin folder of the website:Google.GData.AccessControl.dllGoogle.GData.Calendar.dllGoogle.GData.Client.dllGoogle.GData.Extensions.dll.

Next, add the References to these files in the website project.

Go to References-Add Reference-Browse, choose files and press OK.

Step 3: Create class to interact with Google Calendar service, using the ASP.NET Google API library.

Our task is to create a class which will return an array of events (array of CalendarEventObject objects) from Google Calendar using information that we will provide: "Google calendar name", "Google account name", "Google account password". CalendarEventObject class is defined by us as follows:

public class CalendarEventObject
{
    public DateTime Date { get; set; }
    public string Title { get; set; }
}

The problem is solved as follows:

public class GoogleCalendar
    {
        private const string CALENDARS_URL = 
	"https://www.Google.com/calendar/feeds/default/owncalendars/full";
        private const string CALENDAR_TEMPLATE = 
	"https://www.Google.com/calendar/feeds/{0}/private/full";
        private string m_CalendarUrl = null;
        private string m_CalendarId = null;
        private readonly CalendarService m_Service = null;
        private readonly string m_CalendarName;
        private readonly string m_UserName;
        private readonly string m_Password;
    public GoogleCalendar(string calendarName, string username, string password)
        {
            m_CalendarName = calendarName;
            m_UserName = username;
            m_Password = password;
            m_Service = new CalendarService("Calendar");
        }
        public CalendarEventObject[] GetEvents()
        {
            try
            {
                if (Authenticate())
                {
                    EventQuery query = new EventQuery(m_CalendarUrl);
                    EventFeed feed = m_Service.Query(query);
                    return (from EventEntry entry in feed.Entries
                            select new CalendarEventObject()
                            {Date = entry.Times[0].StartTime, 
				Title = entry.Title.Text}).ToArray();
                }
                else
                {
                    return new CalendarEventObject[0];
                }
            }
            catch (Exception)
            {
                return new CalendarEventObject[0];
            }
        }

      private bool Authenticate()
        {
            m_Service.setUserCredentials(m_UserName, m_Password);
            return SaveCalendarIdAndUrl();
        }

        private bool SaveCalendarIdAndUrl()
        {
            CalendarQuery query = new CalendarQuery();
            query.Uri = new Uri(CALENDARS_URL);
            CalendarFeed resultFeed = (CalendarFeed)m_Service.Query(query);

            foreach (CalendarEntry entry in resultFeed.Entries)
            {
                if (entry.Title.Text == m_CalendarName)
                {
                    m_CalendarId = entry.Id.AbsoluteUri.Substring(63);
                    m_CalendarUrl = string.Format(CALENDAR_TEMPLATE, m_CalendarId);
                    return true;
                }
            }
            return false;
        }

   public string GetCalendarId()
    {
          return m_CalendarId;
    }
}

Description of Variables and Constants

  • CALENDARS_URL - URL where you can get an array of all calendars that the specified account has
  • CALENDAR_TEMPLATE - URL to specific calendar without specifying Id of calendar so far
  • m_CalendarUrl - URL to specific calendar
  • m_CalendarId - Id of specific calendar
  • m_Service - Represents a client connection to a Calendar service
  • m_CalendarName - Represents Google Calendar name
  • m_UserName - Represents Google account name
  • m_Password - Represent Google account password

Description of Constructor and Methods

Constructor

Here is the creation of CalendarService. When you create CalendarService, you provide the name of your application, that is, in fact, any string.

m_Service=new CalendarService("Calendar"); 
Authenticate()

The .NET client library can be used to work with either public or private calendars. Public calendars are read-only and do not require authentication. When you work with private calendars, you need to be authenticated. To authenticate, invoke the http://google-gdata.googlecode.com/svn/docs/folder19/M_Google_GData_Client_Service_setUserCredentials.htmhttp://google-gdata.googlecode.com/svn/docs/folder19/M_Google_GData_Client_Service_setUserCredentials.htm method of CalendarService, specifying the user name and password of Google account:

m_Service.setUserCredentials(m_UserName, m_Password);

Also Authenticate() method invokes SaveCalendarIdAndUrl() helper method to save target Calendar Id and Calendar URL.

SaveCalendarIdAndUrl()

Here is searching for the appropriate calendar in the account's calendars and saving the found Id and URL of Calendar in internal variables.

To request all calendars in account, we create object of CalendarQuery class, set its Uri property, and ask for calendars using Query method of CalendarService:

CalendarQuery query = new CalendarQuery();
query.Uri = new Uri(CALENDARS_URL);
CalendarFeed resultFeed = (CalendarFeed)m_Service.Query(query); 

Then we iterate through account's calendars and search by name for calendar that we need. If we found calendar, then we save Calendar Id and Calendar URL:

 foreach (CalendarEntry entry in resultFeed.Entries)
            {
                if (entry.Title.Text == m_CalendarName)
                {
                    m_CalendarId = entry.Id.AbsoluteUri.Substring(63);
                    m_CalendarUrl = string.Format(CALENDAR_TEMPLATE, m_CalendarId);
                    return true;
                }
            }

One tricky moment may be noted:

m_CalendarId = entry.Id.AbsoluteUri.Substring(63);

Thus, Id rips out from the full Calendar URL. Why so tricky? Often Calendar Id equals Calendar Name. But it is not always true. So this is the most reliable way for me to get Calendar Id for all cases. And it works nice.

GetEvents()

The method returns an array of CalendarEventObject objects(array of events, read from the Google Calendar).

To request events from a specific calendar (this calendar has URL saved before in m_CalendarUrl variable), we create object of EventQuery class and then ask for calendar events using Query method of CalendarService. Then we get custom array of CalendarEventObject objects from Google EventEntry array, and return it:

EventQuery query = new EventQuery(m_CalendarUrl);
EventFeed feed = m_Service.Query(query);
return (from EventEntry entry in feed.Entries
       select new CalendarEventObject()
          {Date = entry.Times[0].StartTime, 
           Title = entry.Title.Text}).ToArray(); 
GetCalendarId()

This method returns Calendar Id saved before in m_CalendarId variable.

(2) Display these events on the site

There are two possible ways for us to display Calendar events:

a) Get array of Calendar events and display it
GoogleCallendar calendar = new GoogleCalendar
	("Google calendar name", "Google account name", "Google account password");
CalendarEventObject[] events= calendar.GetEvents()

So, we have an array of events (CalendarEventObject[]). Now we can go through array and display events on the page as we like. The result may be, for example, as follows:

Calendar
b)Display Google Calendar on the site using Google Calendar iframe

In order to display Google Calendar on your site, paste the following code in aspx page:

<iframe src="https://www.Google.com/calendar/embed?src=<%=
GetCalendarID()%>&ctz=Europe%2FMoscow" style="border: 0" width="800"
height="600" frameborder="0" scrolling="no"></iframe>

After that, define GetCalendarID() method in the Code-behind file of this page. This method returns Id of Calendar:

public string GetCalendarId()
{
     GoogleCalendar calendar = new GoogleCalendar
	("Google calendar name", "Google account name", "Google account password");
     return calendar.GetCalendarId();
}

The result may be, for example, as follows:

Calendar

dipa ahuja replied to Prat Mandav on 03-Oct-11 02:23 AM

If you choose to share your calendar with others, you can create a webpage where they can browse through your calendar — even if they don’t have a Google Calendar account. You can also embed an interactive calendar of your events into your website or blog.

Here’s how:

  1. Login and click the small triangle next to the name of the calendar you want to share on the left side of your screen, and choose “Calendar Settings”.
  2. Click the HTML icon next to “Calendar Address”.
  3. Create a webpage where people can browse through your calendar:
    • Your calendar is available for viewing at the main link listed in the popup box. You can share this link to your calendar with anyone you want.
  4. Embed your calendar into your site or blog:
    • Click the “configuration tool” link from the popup box.
    • Configure your embeddable calendar to your liking, and paste the HTML created at the bottom of the page into the design of your website or blog.

http://www.google.com/googlecalendar/event_publisher_guide.html#toc-site

 

Reena Jain replied to Prat Mandav on 03-Oct-11 02:23 AM
Hi,

Google Calendar allows client applications to view and update calendar events in the form of Google Data API feeds. Your client application can use the Google Calendar Data API to create new events, edit or delete existing events, and query for events that match particular criteria.

There are many possible uses for the Calendar Data API. For example, you can create a web front end for your group's calendar that uses Google Calendar as a back end. Or you can generate a public calendar for Google Calendar to display, based on your organization's event database. Or you can search relevant calendars to display a list of upcoming events on those calendars.

This document provides detailed examples for using the http://code.google.com/p/google-gdata/downloads/list to work with the Google Calendar service. For help setting up the client library, see the http://code.google.com/apis/gdata/articles/dotnet_client_lib.html. You will find examples of adding events, updating events, deleting events and querying events. If you're interested in understanding more about the underlying protocol used by the .NET Client Library to interact with the Calendar Data API, please see the http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html.

http://code.google.com/apis/calendar/data/2.0/developers_guide_dotnet.html
Prat Mandav replied to Web Star on 03-Oct-11 02:24 AM
thanks for reply,looks meaningful

hope it helps
DL M replied to Prat Mandav on 03-Oct-11 02:45 AM
you will need to get a copy of the Google .NET Client APIs – from here http://code.google.com/apis/gdata/client-cs.html. Click on the Download link and select “Google Data API Setup (1.6.0.0).msi” or newer version if available. This is also a good place to find the API docs and related documentation. I found http://code.google.com/apis/calendar/data/2.0/developers_guide_dotnet.html useful. Read Here http://www.kajabity.com/2011/01/adding-calendar-events-to-google-calendar-in-c/
Anoop S replied to Prat Mandav on 03-Oct-11 03:40 AM
you can go ahead and read the documentation provided by Google at
http://code.google.com/apis/calendar/data/2.0/developers_guide_dotnet.html

http://ajax.phpmagazine.net/2006/04/howto_integrate_google_calenda.html may help you in adding events of Google calendar and
http://www.devsource.com/c/a/Techniques/Interacting-with-Google-Calendar-via-NET/ should give you a step wise implementation of Google calendar.

Hope after going through all of the above links, you would be able to successfully integrate the Google Calendar in your website.
bruce harvey replied to Anoop S on 07-Aug-13 05:52 AM
http://www.kettic.com/winforms_ui/csharp_guide/calendar_get_started.shtml