this blog contains information for .net and sql stuffs. You can find various tips and tricks to overcome problem you may be facing in ...

Monday, October 1, 2012

Add Event/Appointment in Google Calendar with Google account credential.


Google calendar is one of the best free tool for tracking your day to day appointment and reminder for your rest of schedule. There are facilities for reminder with email and sms that will enable you for preparing your next meeting/appointment.
We can leverage this tool in our any of application, we can sync our event or appointment with google calendar so one may miss reminder from application but alternatively get from google calendar.
Doesn’t it seem cool? Even implementation is simpler than it seem.
Firstly, you will need to add reference of following files.

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Calendar;

You can find those files here.

Below is code block for adding event in google calendar.

public CalendarService GetService(string applicationName, string userName, string password)
        {
            CalendarService service = new CalendarService(applicationName);
            service.setUserCredentials(userName, password);
            return service;
        }

public static void AddEvent(CalendarService service, string title, string contents, string location, DateTime startTime, DateTime endTime)
        {
            Google.GData.Calendar.EventEntry entry = new Google.GData.Calendar.EventEntry();

            // Set the title
            entry.Title.Text = title;

            //Set event details.
            entry.Content.Content = contents;

            // Set a location for the event.
            Where eventLocation = new Where();
            eventLocation.ValueString = location;
            entry.Locations.Add(eventLocation);

            //Set starting time and end time for the event.
            When eventTime = new When(startTime, endTime);
            entry.Times.Add(eventTime);

            Uri postUri = new Uri("https://www.google.com/calendar/feeds/aslam.h.net@gmail.com/private/full");

            // Send the request and receive the response:
            AtomEntry insertedEntry = service.Insert(postUri, entry);
        }

Here, GetService method will take three parameters named application name, username and password.
Application name can be any name, you wish. Username is your google account name where you have configured your calendar and wish to sync with that calendar. Password is your google account password.
Method will return back calendar service object. We can use this service object anywhere for using any service associated with calendar.

Method AddEvent: this method will add event in any our calendar. Most of statements are formatted with comments.
There is one critical statement you need to take care.

Uri postUri = new Uri("https://www.google.com/calendar/feeds/shailesh@gmail.com/private/full");


Please note that one dynamic part, shailesh@gmail.com. It’s my calendar’s id. You can easily find your sync calendar id with following steps.

1)      Logon to your gmail account and click on calendar link at top bar, it will open up your calendar in new tab.
2)      On the right side, there will be section named “My Calendars”. It lists out calendars, you created. You can create more than one calendar in google calendar tool.
3)      Go to any of calendar, click on the arrow. It will show one menu. Click on “Calendar settings”.
4)      There will be one row with “Calendar Address”, most probably before last row. Just look around this row, there will be information like this.
(Calendar ID: shailesh @gmail.com)
This is the address for your calendar. No one can use this link unless you have made your calendar public

That’s it.

I hope this will enable to sync your important tasks, appointment and meeting with google calendar.

Happy scheduling appointment