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");
string eventText = this.EventTitle;
if (string.IsNullOrEmpty(eventText))
{
eventText = this.Text;
}
if (!string.IsNullOrEmpty(eventText))
{
url.AppendFormat("&text={0}",
HttpUtility.UrlEncode(eventText));
}
url.Append("&dates=");
if (this.StartDateTime != null)
{
if (this.AllDayEvent || (this.StartDateTime == this.EndDateTime))
{
url.AppendFormat("{0}/{0}",
this.StartDateTime.ToString("yyyyMMdd"));
}
else
{
const string UTCFORMATSTRING = "yyyyMMdd\\THHmmss\\Z";
url.AppendFormat("{0}/{1}",
this.StartDateTime.ToUniversalTime().ToString(UTCFORMATSTRING),
this.EndDateTime.ToUniversalTime().ToString(UTCFORMATSTRING));
}
}
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));
}
if (!string.IsNullOrEmpty(this.EventLocation))
{
url.AppendFormat("&location={0}",
HttpUtility.UrlEncode(this.EventLocation));
}
if (!string.IsNullOrEmpty(this.EventDescription))
{
url.AppendFormat("&details={0}",
HttpUtility.UrlEncode(this.EventDescription));
}
if (this.MarkAsBusy)
{
url.AppendFormat("&trp={0}", this.MarkAsBusy);
}
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