How To Use Google Calendar in ASP.NET Through Google API

Introduction

In this article, I will describe how to use Google Calendar in an ASP.Net website.

Now let us see the procedure for including Google calendar.

Step 1

First, we download the .Net library for Google data API from Google_Data_API_Setup_1.8.00.msi and install it.

Download from this link

Step 2

By default these files are stored in the "C:\Program Files (x86)\Google\Google Data API SDK\Redist" folder. Now we must copy some of the files, which are Google.GData.AccessControl.dll, Google.GData.Calendar.dll, Google.GData.Client.dll and Google.GData.Extensions.dll into the bin folder of the website.

Step 3

Add the references of these files in the website. Right-click on the "References" node in the Solution Explorer then select "Add reference" then select the "Browse" tab then select the files and press "OK".

Step 4

We will now develop the code.

In this code we have the following information:

g_CalendarName- store the Google calendar name.
g_UserName-  Store Google user account name.
g_Password- Store Google user account password.
g_CalUrl- Google calendar URL.
g_CalId-Google calendar Id.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using Google.GData.Calendar;  
  6. using Google.GData.Extensions;  
  7. using Google.GData.AccessControl;  
  8. using Google.GData.Client;  
  9.   
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  12.    protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.     }  
  15.     public class Calendar  
  16.     {  
  17.         public DateTime dt { getset; }  
  18.         public string tle { getset; }  
  19.     }  
  20.     public class GCalendar  
  21.     {  
  22.         private const string CAL_URL =  
  23. "https://www.Google.com/calendar/feeds/default/owncalendars/full";  
  24.         private const string CAL_TEMPLATE =  
  25.     "https://www.Google.com/calendar/feeds/{0}/private/full";  
  26.         private string g_CalUrl = null;  
  27.         private string g_CalId = null;  
  28.         private readonly CalendarService g_calService = null;  
  29.         private readonly string g_CalendarName;  
  30.         private readonly string g_UserName;  
  31.         private readonly string g_Password;  
  32.        public GCalendar(string cal_Name, string user_name, string user_password)  
  33.         {  
  34.             g_CalendarName = cal_Name;  
  35.             g_UserName = user_name;  
  36.             g_Password = user_password;  
  37.             g_calService = new CalendarService("Calendar");  
  38.         }  
  39.         public Calendar[] GetEvents()  
  40.         {  
  41.             try  
  42.             {  
  43.                 if (google_authentication())  
  44.                 {  
  45.                     EventQuery qry = new EventQuery(g_CalUrl);  
  46.                     EventFeed fd = g_calService.Query(qry);  
  47.                     return (from EventEntry entry in fd.Entries  
  48.                             select new Calendar()  
  49.                             {  
  50.                                 dt = entry.Times[0].StartTime,  
  51.                                 tle = entry.Title.Text}).ToArray();  
  52.                 }  
  53.                 else  
  54.                 {  
  55.                     return new Calendar[0];  
  56.   
  57.                 }  
  58.             }  
  59.             catch (Exception)  
  60.             {  
  61.                 return new Calendar[0];  
  62.             }  
  63.         }  
  64.        private bool google_authentication()  
  65.        {  
  66.             g_calService.setUserCredentials(g_UserName, g_Password);  
  67.             return SaveCalIdAndUrl();  
  68.        }  
  69.        private bool SaveCalIdAndUrl()  
  70.        {  
  71.             CalendarQuery qry = new CalendarQuery();  
  72.     qry.Uri = new Uri(CAL_URL);  
  73.             CalendarFeed resultFeed = (CalendarFeed)g_calService.Query(qry);  
  74.             foreach (CalendarEntry entry in resultFeed.Entries)  
  75.             {  
  76.                 if (entry.Title.Text == g_CalendarName)  
  77.                 {  
  78.                     g_CalId = entry.Id.AbsoluteUri.Substring(63);  
  79.                     g_CalUrl = string.Format(CAL_TEMPLATE,g_CalId);  
  80.                     return true;  
  81.                 }  
  82.             }  
  83.             return false;  
  84.         }  
  85.         public string CalId()  
  86.         {  
  87.             return g_CalId;  
  88.         }  
  89.     }  
  90. } 

We use the get and set methods for date and title. That sets the date and title of the calendar using the set method and returns the date and title of the calendar using the set method.

We create a CalendarService in which we provide the name of the application, that we will use.

We use the google_authentication method to call the savecalIdAndUrl() method that stores the id and URL of the calendar.

Step 5

Now we create a CalId() method that returns the id of the calendar.

We create a GCalendar object that access the CalId() method and returns the calendar method; see:

  1. public string CalId()  
  2. {  
  3.     GCalendar cal = new GCalendar("Google calendar name""Google account name""Google account password");  
  4.     return cal.CalId();  
  5. } 

Step 6

To display the Google Calendar on your website we create this code, in which the id is passes by the CalId() method.

And write this code in the .aspx page:

  1. <iframe src="https://www.Google.com/calendar/embed?src=<%#  
  2. CalId()%>&ctz=Europe%2FMoscow" style="border-style: none; border-color: inherit; border-width: 0; width: 824px;" height="600" frameborder="0" scrolling="no"></iframe>

Now we display the complete source code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using Google.GData.Calendar;  
  6. using Google.GData.Extensions;  
  7. using Google.GData.AccessControl;  
  8. using Google.GData.Client;  
  9.   
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.     }  
  15.     public class Calendar  
  16.     {  
  17.         public DateTime dt { getset; }  
  18.         public string tle { getset; }  
  19.     }  
  20.     public class GCalendar  
  21.     {  
  22.         private const string CAL_URL =  
  23.     "https://www.Google.com/calendar/feeds/default/owncalendars/full";  
  24.         private const string CAL_TEMPLATE =  
  25.     "https://www.Google.com/calendar/feeds/{0}/private/full";  
  26.         private string g_CalUrl = null;  
  27.         private string g_CalId = null;  
  28.         private readonly CalendarService g_calService = null;  
  29.         private readonly string g_CalendarName;  
  30.         private readonly string g_UserName;  
  31.         private readonly string g_Password;  
  32.         public GCalendar(string cal_Name, string user_name, string user_password)  
  33.         {  
  34.             g_CalendarName = cal_Name;  
  35.             g_UserName = user_name;  
  36.             g_Password = user_password;  
  37.             g_calService = new CalendarService("Calendar");  
  38.         }  
  39.         public Calendar[] GetEvents()  
  40.         {  
  41.             try  
  42.             {  
  43.                 if (google_authentication())  
  44.                 {  
  45.                     EventQuery qry = new EventQuery(g_CalUrl);  
  46.                     EventFeed fd = g_calService.Query(qry);  
  47.                     return (from EventEntry entry in fd.Entries  
  48.                             select new Calendar()  
  49.                             {  
  50.                                 dt = entry.Times[0].StartTime,  
  51.                                 tle = entry.Title.Text  
  52.                             }).ToArray();  
  53.                 }  
  54.                 else  
  55.                 {  
  56.                     return new Calendar[0];  
  57.                 }  
  58.             }  
  59.             catch (Exception)  
  60.             {  
  61.                 return new Calendar[0];  
  62.             }  
  63.         }  
  64.         private bool google_authentication()  
  65.         {  
  66.             g_calService.setUserCredentials(g_UserName, g_Password);  
  67.             return SaveCalIdAndUrl();  
  68.         }  
  69.         private bool SaveCalIdAndUrl()  
  70.         {  
  71.             CalendarQuery qry = new CalendarQuery();  
  72.             qry.Uri = new Uri(CAL_URL);  
  73.             CalendarFeed resultFeed = (CalendarFeed)g_calService.Query(qry);  
  74.             foreach (CalendarEntry entry in resultFeed.Entries)  
  75.             {  
  76.                 if (entry.Title.Text == g_CalendarName)  
  77.                 {  
  78.    g_CalId = entry.Id.AbsoluteUri.Substring(63);  
  79.                     g_CalUrl = string.Format(CAL_TEMPLATE,g_CalId);  
  80.                     return true;  
  81.                 }  
  82.             }  
  83.             return false;  
  84.         }  
  85.         public string CalId()  
  86.         {  
  87.    return g_CalId;  
  88.         }  
  89.     }  
  90.     public string CalId()  
  91.     {  
  92.         GCalendar cal = new GCalendar  
  93.        ("Google calendar name""Google account name""Google account password");  
  94.         return cal.CalId();  
  95.     }   
  96. } 

Output
 

Clipboard02.jpg
 


Similar Articles