Click here to Skip to main content
15,890,897 members
Articles / Web Development / ASP.NET

ASP.NET Custom Control to Create Google Calendar Event Hyperlinks

Rate me:
Please Sign up or sign in to vote.
4.06/5 (6 votes)
13 Apr 2006CPOL 117.8K   570   40   4
An ASP.NET Custom Control to create Google Calendar Event hyperlinks.

Sample Image - GoogleCalendarEventLink.gif

Introduction

Google recently released the Google Calendar application. If your website has event information, allow users to add the event to their own Google Calendar easily with a Google Calendar event link.

Example link

What I've done is created a new ASP.NET HyperLink control, and added some properties that allow specific information to be attached to the HyperLink control for a Google Calendar Event. Then, I provided an overridden Render() function that assembles the specific NavigateUrl with the provided information, supplying defaults if necessary.

Render() code

C#
protected override void Render(HtmlTextWriter writer)
{
    StringBuilder url = new StringBuilder();
    url.Append("http://www.google.com/calendar/event?");
    url.Append("action=TEMPLATE");

    // Event title

    string eventText = this.EventTitle;
    if (string.IsNullOrEmpty(eventText))
    {
        eventText = this.Text;
    }
    if (!string.IsNullOrEmpty(eventText))
    {
        url.AppendFormat("&text={0}", 
                         HttpUtility.UrlEncode(eventText));
    }

    // Event dates

    // TODO: Validate that a start or end date has been specified

    url.Append("&dates=");
    if (this.StartDateTime != null)
    {
        if (this.AllDayEvent || (this.StartDateTime == this.EndDateTime))
        {
            url.AppendFormat("{0}/{0}", 
                this.StartDateTime.ToString("yyyyMMdd"));
        }
        else
        {
            // TODO: Validate that EndDateTime is set,
            // because this is not an all day event

                const string UTCFORMATSTRING = "yyyyMMdd\\THHmmss\\Z";
                url.AppendFormat("{0}/{1}", 
                  this.StartDateTime.ToUniversalTime().ToString(UTCFORMATSTRING),
                  this.EndDateTime.ToUniversalTime().ToString(UTCFORMATSTRING));
            }
        }

        // TODO: Apparently on sprop is required by google,
        // so validate that one is specified
        // Organization info

        if (!string.IsNullOrEmpty(this.OrganizerName))
        {
            url.AppendFormat("&sprop=name:{0}", 
                HttpUtility.UrlEncode(this.OrganizerName));
        }
        if (!string.IsNullOrEmpty(this.OrganizerWebsite))
        {
            url.AppendFormat("&sprop=website:{0}", 
                HttpUtility.UrlEncode(this.OrganizerWebsite));
        }

        // Event location

        if (!string.IsNullOrEmpty(this.EventLocation))
        {
            url.AppendFormat("&location={0}", 
                HttpUtility.UrlEncode(this.EventLocation));
        }

        // Event description

        if (!string.IsNullOrEmpty(this.EventDescription))
        {
            url.AppendFormat("&details={0}", 
                HttpUtility.UrlEncode(this.EventDescription));
        }

        // Free/Busy
        // Only add to url if true since default false
        // and url could already be really long

        if (this.MarkAsBusy)
        {
            url.AppendFormat("&trp={0}", this.MarkAsBusy);
        }

        // Set the NavigateUrl

        this.NavigateUrl = url.ToString();
        base.Render(writer);
    }
}

This is my first stab at a server control (I usually just write user controls), so let me know if there are problems, but please be gentle :)

See also

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
http://www.onlinescorekeeper.com/

Comments and Discussions

 
GeneralCan't run the demo Pin
Rens Duijsens29-Jun-08 4:47
Rens Duijsens29-Jun-08 4:47 
Hallo,

I have tried the demo.
I think I'm missing a part of the development enviroment.
The 'using Google.xxxx' statement produce an error, because they aren't there.

How do I get everything to work.

Friendly greetings

Rens Duijsens

P.s. Are the VB.NET 2008 examples somewhere out there please?
GeneralRe: Can't run the demo Pin
slolife30-Jun-08 7:00
slolife30-Jun-08 7:00 
GeneralA few Suggestions Pin
James Curran14-Apr-06 5:46
James Curran14-Apr-06 5:46 
GeneralRe: A few Suggestions Pin
slolife14-Apr-06 5:56
slolife14-Apr-06 5:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.