Home > Code > C# > Add, Delete, Update or Move Appointments in Google Calendars using .NET

Add, Delete, Update or Move Appointments in Google Calendars using .NET

by sher azam   on Mar 05, 2014   Category: C#   |  Views: 1849    |  Points: 25   |  Gold 


This technical tip explains how developers can use Gmail Calendars to add, retrieve delete, move or update Appointments in their own applications using Aspose.Email. Aspose.Email provides features for working with Appointments in Google calendars. Following is the list of tasks that can be performed on appointments in Google calendar:

1. Add Appointments
2. Retrieve list of appointments
3. Retrieve particular appointment
4. Update an appointment
5. Move appointment from one calendar to another
6. Delete appointment

Adding an appointment

Following code sample demonstrates the feature of adding an appointment in a calendar.


//get access token
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken = GoogleOAuthHelper.GetAccessToken(User2);
//Get IGmailclient
using (IGmailClient client = Aspose.Email.Google.GmailClient.GetInstance(accessToken))
{
//Create local calendar
Calendar calendar1 = new Calendar(
"summary - " + Guid.NewGuid().ToString(),
null,
null,
"Europe/Kiev");
//Insert calendar and get id of inserted calendar
string id = client.CreateCalendar(calendar1);

//Get back calendar using an id
Calendar cal1 = client.FetchCalendar(id);
string calendarId1 = cal1.Id;
try
{
//Retrieve list of appointments from the first calendar
Appointment[] appointments = client.ListAppointments(calendarId1);

//It shall be zero
if (appointments.Length > 0)
{
Console.WriteLine("Wrong number of appointments");
return;
}

//Get current time and Calculate time after an hour from now
DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddHours(1);

//Initialize a mail address collection and set attendees mail address
MailAddressCollection attendees = new MailAddressCollection();
attendees.Add("User1.EMail@domain.com");
attendees.Add("User3.EMail@domain.com");

//create an appointment with above attendees
Appointment app1 = new Appointment(
"Location - " + Guid.NewGuid().ToString(),
startDate,
endDate,
User2.EMail,
attendees);

//Set appointment summary, description, start/end time zone
app1.Summary = "Summary - " + Guid.NewGuid().ToString();
app1.Description = "Description - " + Guid.NewGuid().ToString();
app1.StartTimeZone = "Europe/Kiev";
app1.EndTimeZone = "Europe/Kiev";

//Insert appointment in the first calendar inserted above and get back inserted appointment
Appointment app2 = client.CreateAppointment(calendarId1, app1);

//Retrieve appointment using unique id
Appointment app3 = client.FetchAppointment(calendarId1, app2.UniqueId);

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}



Retrieve and update appointment

Here retrieving and updating of calendar is demonstrated as follows:

static void UpdateAppointment(string calendarId, string AppointmentUniqueId)
{
//get access token
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken = GoogleOAuthHelper.GetAccessToken(User2);
//Get IGmailclient
using (IGmailClient client = Aspose.Email.Google.GmailClient.GetInstance(accessToken))
{
//Retrieve Appointment
Appointment app3 = client.FetchAppointment(calendarId, AppointmentUniqueId);
//Change the appointment information
app3.Summary = "New Summary - " + Guid.NewGuid().ToString();
app3.Description = "New Description - " + Guid.NewGuid().ToString();
app3.Location = "New Location - " + Guid.NewGuid().ToString();
app3.Flags = AppointmentFlags.AllDayEvent;
app3.StartDate = DateTime.Now.AddHours(2);
app3.EndDate = app3.StartDate.AddHours(1);
app3.StartTimeZone = "Europe/Kiev";
app3.EndTimeZone = "Europe/Kiev";

//Update the appointment and get back updated appointment
Appointment app4 = client.UpdateAppointment(calendarId, app3);
}
}



Move and Delete appointment

Appointment can be moved by providing the source calendar, destination calendar and unique id of appointment in the source calendar.

static void MoveDeleteAppointment(string SourceCalendarId, string DestinationCalendarId, string TargetAppUniqueId)
{
//get access token
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken = GoogleOAuthHelper.GetAccessToken(User2);
//Get IGmailclient
using (IGmailClient client = Aspose.Email.Google.GmailClient.GetInstance(accessToken))
{
//Retrieve the list of appointments in the destination calendar before moving the appointment
Appointment[] appointments = client.ListAppointments(DestinationCalendarId);
Console.WriteLine("Before moving count = " + appointments.Length);

Appointment Movedapp = client.MoveAppointment(SourceCalendarId, DestinationCalendarId, TargetAppUniqueId);

//Retrieve the list of appointments in the destination calendar after moving the appointment
appointments = client.ListAppointments(DestinationCalendarId);
Console.WriteLine("After moving count = " + appointments.Length);

//Delete particular appointment from a calendar using unique id
client.DeleteAppointment(DestinationCalendarId, Movedapp.UniqueId);

//Retrieve the list of appointments. It should be one less than the earlier appointments in the destination calendar
appointments = client.ListAppointments(DestinationCalendarId);
Console.WriteLine("After deleting count = " + appointments.Length);

}
}



Import appointment and retrieve instances

Appointment can be imported by providing the source calendar and appointment.


//get access token
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken = GoogleOAuthHelper.GetAccessToken(User2);
//Get IGmailClient
using (IGmailClient client = Aspose.Email.Google.GmailClient.GetInstance(accessToken))
{
//Initialize calendar object
Calendar calendar1 = new Calendar("summary - " + Guid.NewGuid().ToString(),null,null,"Europe/Kiev");

//Insert calendar and get back id of inserted calendar
string id = client.CreateCalendar(calendar1);

//Fetch same calendar using id
Calendar cal1 = client.FetchCalendar(id);

string calendarId1 = cal1.Id;
try
{
//Get list of appointments from the calendar. it should be zero.
Appointment[] appointments = client.ListAppointments(calendarId1);
if (appointments.Length != 0)
{
Console.WriteLine("Wrong number of appointments.");
}

//Get current time as appointment start time and add an hour to calculate appointment fisnih time
DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddHours(1);

//Initialize attendees list
MailAddressCollection attendees = new MailAddressCollection();
attendees.Add("user1@domain.com");
attendees.Add("user2@domain.com");

//Create an appointment
Appointment app1 = new Appointment("Location - " + Guid.NewGuid().ToString(),startDate,endDate,server.User2.EMail,attendees);
app1.Summary = "Summary - " + Guid.NewGuid().ToString();
app1.Description = "Description - " + Guid.NewGuid().ToString();
app1.StartTimeZone = "Europe/Kiev";
app1.EndTimeZone = "Europe/Kiev";

//Insert appointment in the calendar and get back same appointment incase of successful insertion
Appointment app2 = client.CreateAppointment(calendarId1, app1);

//import the appointment from calendar 1
Appointment app3 = client.ImportAppointment(calendarId1, app2);

//Get list of appointment instances in calendar using appointment unique id
Appointment[] instances = client.ListAppointmentInstances(calendarId1, app2.UniqueId);

//get list of appointments. It should be one
appointments = client.ListAppointments(calendarId1);
if (appointments.Length != 1)
{
Console.WriteLine("Wrong number of appointments");
return;
}

//Delete appointment from calendar
client.DeleteAppointment(calendarId1, app2.UniqueId);

//Get list of appointments from the calendar. It should be zero now.
appointments = client.ListAppointments(calendarId1);
if (appointments.Length != 0)
{
Console.WriteLine("Wrong number of appointments");
return;
}
}
finally
{
//Delete calendar
client.DeleteCalendar(cal1.Id);
}
}


Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts