Click here to Skip to main content
5,785,816 members and growing! (19,310 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

ASP.NET Custom control to create Google Calendar Event hyperlinks

By slolife

ASP.NET Custom control to create Google Calendar Event hyperlinks
Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 13 Apr 2006
Updated: 13 Apr 2006
Views: 31,798
Bookmarked: 29 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 3.16 Rating: 4.06 out of 5
0 votes, 0.0%
1
2 votes, 33.3%
2
1 vote, 16.7%
3
0 votes, 0.0%
4
3 votes, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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

        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)

About the Author

slolife


http://www.onlinescorekeeper.com/
Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralCan't run the demomemberRens Duijsens5:47 29 Jun '08  
GeneralRe: Can't run the demomemberslolife8:00 30 Jun '08  
GeneralA few SuggestionsmemberJames Curran6:46 14 Apr '06  
GeneralRe: A few SuggestionsmemberChris Becker6:56 14 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Apr 2006
Editor:
Copyright 2006 by slolife
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project