Monday, February 6, 2012

Integrating Google(gmail) calendar in ASP.NET application

The below article will provide you a small snippet of code,that can be used to intgrate the gmail calendar in you asp.net application.Basically,You need to pass the parameters like Username,Password,StartDate,EndDate to retrieve the appointment list.


I have created a custom class"CalendarInfo" to hold the appointment list.

"CalendarInfo" class contains the below properties.

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.Serialization;

[Serializable()]

public class CalendarInfo

{

private string _location;

private DateTime _startTime;

private DateTime _endTime;

private string _subject;

private string _body;

 



public string Location

{

get { return _location; }

set { _location = value; }

}

public DateTime StartTime

{

get { return _startTime; }

set { _startTime = value; }

}

public DateTime EndTime

{

get { return _endTime; }

set { _endTime = value; }

}

public string Subject

{

get { return _subject; }

set { _subject = value; }

}

public string Body

{

get { return _body; }

set { _body = value; }

}

}



private void GoogleCalendar(string username, string password, DateTime startdate, DateTime enddate)

{

       
List<CalendarInfo> _calendarEvents = new List<CalendarInfo>();
//pass logged in user credentials

CalendarService service = new CalendarService("exampleCo-exampleApp-1");

service.setUserCredentials(username, password);

EventQuery query = new EventQuery();

query.Uri = new Uri("https://www.google.com/calendar/feeds/default/private/full");

query.SortOrder = CalendarSortOrder.ascending;

try

{

EventFeed events = service.Query(query);

int n = events.Entries.Count;

if (events.Entries.Count > 0)

{

int x = 0;

string mes = string.Empty;

while (x < n)

{

Google.GData.Calendar.
EventEntry entry = (Google.GData.Calendar.EventEntry)events.Entries[x];

if (entry.Times[0].StartTime.Date >= startdate && entry.Times[0].EndTime.Date <= enddate)

{



CalendarInfo objinfo = new CalendarInfo();

objinfo.Subject = entry.Title.Text;

objinfo.Body = entry.Content.Content;

objinfo.StartTime =
Convert.ToDateTime(entry.Times[0].StartTime);

objinfo.EndTime = Convert.ToDateTime(entry.Times[0].EndTime);

objinfo.Location = entry.Locations[0].ValueString;

_calendarEvents.Add(objinfo);



}

x = x + 1;

}

}

}

catch (GDataRequestException ex)

{
  //write the logic to log the exception
}

catch (GDataBatchRequestException ex)

{
    //write the logic to log the exception
}

catch (ArgumentOutOfRangeException ex)

{
    //write the logic to log the exception
}

catch (ClientFeedException ex)

{
    //write the logic to log the exception
}

catch (AuthenticationException ex)

{
    //write the logic to log the exception
}

}

No comments:

Post a Comment