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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Google.GData.Calendar;
using Google.GData.Extensions;
using Google.GData.AccessControl;
using Google.GData.Client;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public class Calendar
{
public DateTime dt { get; set; }
public string tle { get; set; }
}
public class GCalendar
{
private const string CAL_URL =
"https://www.Google.com/calendar/feeds/default/owncalendars/full";
private const string CAL_TEMPLATE =
"https://www.Google.com/calendar/feeds/{0}/private/full";
private string g_CalUrl = null;
private string g_CalId = null;
private readonly CalendarService g_calService = null;
private readonly string g_CalendarName;
private readonly string g_UserName;
private readonly string g_Password;
public GCalendar(string cal_Name, string user_name, string user_password)
{
g_CalendarName = cal_Name;
g_UserName = user_name;
g_Password = user_password;
g_calService = new CalendarService("Calendar");
}
public Calendar[] GetEvents()
{
try
{
if (google_authentication())
{
EventQuery qry = new EventQuery(g_CalUrl);
EventFeed fd = g_calService.Query(qry);
return (from EventEntry entry in fd.Entries
select new Calendar()
{
dt = entry.Times[0].StartTime,
tle = entry.Title.Text
}).ToArray();
}
else
{
return new Calendar[0];
}
}
catch (Exception)
{
return new Calendar[0];
}
}
private bool google_authentication()
{
g_calService.setUserCredentials(g_UserName, g_Password);
return SaveCalIdAndUrl();
}
private bool SaveCalIdAndUrl()
{
CalendarQuery qry = new CalendarQuery();
qry.Uri = new Uri(CAL_URL);
CalendarFeed resultFeed = (CalendarFeed)g_calService.Query(qry);
foreach (CalendarEntry entry in resultFeed.Entries)
{
if (entry.Title.Text == g_CalendarName)
{
g_CalId = entry.Id.AbsoluteUri.Substring(63);
g_CalUrl = string.Format(CAL_TEMPLATE,g_CalId);
return true;
}
}
return false;
}
public string CalId()
{
return g_CalId;
}
}
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() methyod that returns the id of the calendar.
We create a GCalendar object that access the CalId() method and returns the calendar method; see:
public string CalId()
{
GCalendar cal = new GCalendar
("Google calendar name", "Google account name", "Google account password");
return cal.CalId();
}
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:
<iframe src="https://www.Google.com/calendar/embed?src=<%#
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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Google.GData.Calendar;
using Google.GData.Extensions;
using Google.GData.AccessControl;
using Google.GData.Client;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public class Calendar
{
public DateTime dt { get; set; }
public string tle { get; set; }
}
public class GCalendar
{
private const string CAL_URL =
"https://www.Google.com/calendar/feeds/default/owncalendars/full";
private const string CAL_TEMPLATE =
"https://www.Google.com/calendar/feeds/{0}/private/full";
private string g_CalUrl = null;
private string g_CalId = null;
private readonly CalendarService g_calService = null;
private readonly string g_CalendarName;
private readonly string g_UserName;
private readonly string g_Password;
public GCalendar(string cal_Name, string user_name, string user_password)
{
g_CalendarName = cal_Name;
g_UserName = user_name;
g_Password = user_password;
g_calService = new CalendarService("Calendar");
}
public Calendar[] GetEvents()
{
try
{
if (google_authentication())
{
EventQuery qry = new EventQuery(g_CalUrl);
EventFeed fd = g_calService.Query(qry);
return (from EventEntry entry in fd.Entries
select new Calendar()
{
dt = entry.Times[0].StartTime,
tle = entry.Title.Text
}).ToArray();
}
else
{
return new Calendar[0];
}
}
catch (Exception)
{
return new Calendar[0];
}
}
private bool google_authentication()
{
g_calService.setUserCredentials(g_UserName, g_Password);
return SaveCalIdAndUrl();
}
private bool SaveCalIdAndUrl()
{
CalendarQuery qry = new CalendarQuery();
qry.Uri = new Uri(CAL_URL);
CalendarFeed resultFeed = (CalendarFeed)g_calService.Query(qry);
foreach (CalendarEntry entry in resultFeed.Entries)
{
if (entry.Title.Text == g_CalendarName)
{
g_CalId = entry.Id.AbsoluteUri.Substring(63);
g_CalUrl = string.Format(CAL_TEMPLATE,g_CalId);
return true;
}
}
return false;
}
public string CalId()
{
return g_CalId;
}
}
public string CalId()
{
GCalendar cal = new GCalendar
("Google calendar name", "Google account name", "Google account password");
return cal.CalId();
}
Output