Friday, 7 December 2012

Exporting events to google calendar Link HtmlHelper


Following is a ASP.NET MVC HtmlHelper to generate an anchor link for exporting an event to Google Calendar.
public static MvcHtmlString ExportEventToGoogleLink(this HtmlHelper htmlHelper, string title, DateTime startDateTime, DateTime endDateTime, string description, string location, string linkText, IDictionary<string, object> htmlAttributes = null)
{
const string dateFormat = "yyyyMMddTHHmmssZ";
Uri url = System.Web.HttpContext.Current.Request.Url;
StringBuilder sb = new StringBuilder();
sb.Append("http://www.google.com/calendar/event?action=TEMPLATE");
sb.Append("&text=" + title);
sb.Append("&dates=" + startDateTime.ToUniversalTime().ToString(dateFormat));
sb.Append("/");
sb.Append(endDateTime.ToUniversalTime().ToString(dateFormat));
sb.Append("&details=" + description);
sb.Append("&location=" + location);
sb.Append("&trp=false");
sb.Append("&sprop=" + url.Scheme + "://" + url.Host);
sb.Append("&sprop=name:" + url.Host);

TagBuilder tagBuilder = new TagBuilder("a");
tagBuilder.MergeAttribute("href", sb.ToString());
tagBuilder.MergeAttribute("target", "_blank");
tagBuilder.InnerHtml = "<span class='event-export-google'>" + linkText + "</span>";
if (htmlAttributes != null)
tagBuilder.MergeAttributes(htmlAttributes, true);
return MvcHtmlString.Create(tagBuilder.ToString());
}


Demo Link

No comments:

Post a Comment