Skip to content

Instantly share code, notes, and snippets.

@MikeWills
Created April 11, 2011 19:06
Show Gist options
  • Save MikeWills/914080 to your computer and use it in GitHub Desktop.
Save MikeWills/914080 to your computer and use it in GitHub Desktop.
I couldn't find an easy way to generate ical or JSON feeds in ASP.NET. This is what I created to get that done.
/*
Class: Calendar
Created by: Mike Wills (http://mikewills.me)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
namespace SupervisorPayroll.Models
{
#region Event
public class Event
{
public string Title { set; get; }
public string Description { set; get; }
public string URL { set; get; }
public string UID { set; get; }
public DateTime DateTimeStamp { set; get; }
public DateTime CreatedDateTime { set; get; }
public DateTime Start { set; get; }
public DateTime End { set; get; }
public bool AllDay { set; get; }
}
#endregion
public class Calendar
{
#region Model
public string CalendarName { set; get; }
public string Product { set; get; }
public string TimeZone { set; get; }
public List<Event> Events { private set; get; }
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="Calendar"/> class.
/// </summary>
public Calendar()
{
Events = new List<Event>();
}
/// <summary>
/// Adds the event.
/// </summary>
/// <param name="eventItem">The event item.</param>
public void AddEvent(Event eventItem)
{
Events.Add(eventItem);
}
/// <summary>
/// Creates an Ical string.
/// </summary>
/// <returns></returns>
public string ToICalString()
{
const string EOL = "\r\n";
StringBuilder sb = new StringBuilder();
sb.Append("BEGIN:VCALENDAR");
sb.Append(EOL);
sb.Append("VERSION:2.0");
sb.Append(EOL);
sb.Append("METHOD:PUBLISH");
sb.Append(EOL);
sb.Append("X-PUBLISHED-TTL:PT60M");
sb.Append(EOL);
sb.AppendFormat("PRODID:{0}", Product.Replace(",", "\\,"));
sb.Append(EOL);
sb.AppendFormat("X-WR-TIMEZONE:{0}", TimeZone);
sb.Append(EOL);
sb.AppendFormat("X-WR-CALNAME:{0}", CalendarName.Replace(",", "\\,"));
sb.Append(EOL);
foreach (Event eventItem in Events)
{
sb.Append("BEGIN:VEVENT");
sb.Append(EOL);
sb.AppendFormat("UID:{0}", eventItem.UID.Replace(",", "\\,"));
sb.Append(EOL);
sb.AppendFormat("DTSTAMP:{0}T{1}Z", eventItem.DateTimeStamp.ToString("yyyyMMdd"), eventItem.DateTimeStamp.ToString("HHmmss"));
sb.Append(EOL);
sb.AppendFormat("CREATED:{0}T{1}Z", eventItem.CreatedDateTime.ToString("yyyyMMdd"), eventItem.CreatedDateTime.ToString("HHmmss"));
sb.Append(EOL);
if (eventItem.AllDay)
{
if (eventItem.End.Date != DateTime.MinValue.Date)
{
sb.AppendFormat("DTSTART:{0}T000000", eventItem.Start.ToString("yyyyMMdd"));
sb.Append(EOL);
sb.AppendFormat("DTEND:{0}T000000", eventItem.End.AddDays(1).ToString("yyyyMMdd"));
sb.Append(EOL);
sb.Append("X-MICROSOFT-CDO-ALLDAYEVENT:TRUE");
sb.Append(EOL);
}
else
{
sb.AppendFormat("DTSTART:{0}T000000", eventItem.Start.ToString("yyyyMMdd"));
sb.Append(EOL);
sb.Append("X-MICROSOFT-CDO-ALLDAYEVENT:TRUE");
sb.Append(EOL);
}
}
else
{
sb.AppendFormat("DTSTART:{0}T{1}", eventItem.Start.ToString("yyyyMMdd"), eventItem.Start.ToString("HHmmss"));
sb.Append(EOL);
sb.AppendFormat("DTEND:{0}T{1}", eventItem.End.ToString("yyyyMMdd"), eventItem.End.ToString("HHmmss"));
sb.Append(EOL);
}
sb.AppendFormat("SUMMARY:{0}", eventItem.Title.Replace(",", "\\,"));
sb.Append(EOL);
sb.AppendFormat("DESCRIPTION:{0}", eventItem.Description.Replace(",", "\\,"));
sb.Append(EOL);
sb.AppendFormat("URL:{0}", eventItem.URL);
sb.Append(EOL);
sb.Append("END:VEVENT");
sb.Append(EOL);
}
sb.Append("END:VCALENDAR");
sb.Append(EOL);
return sb.ToString();
}
/// <summary>
/// Creates a calendar json string.
/// </summary>
/// <returns></returns>
public string ToFullCalendarJsonString()
{
StringBuilder sb = new StringBuilder();
sb.Append("[");
int count = Events.Count;
int i = 0;
foreach (Event eventItem in Events)
{
sb.Append("{");
sb.AppendFormat("\"title\":\"{0}\",", eventItem.Title);
sb.AppendFormat("\"allDay\":\"{0}\",", eventItem.AllDay);
if (eventItem.AllDay)
{
sb.AppendFormat("\"start\":\"{0}T00:00:00\",", eventItem.Start.ToString("yyyy-MM-dd"));
sb.AppendFormat("\"end\":\"{0}T00:00:00\",", eventItem.End.ToString("yyyy-MM-dd"));
}
else
{
sb.AppendFormat("\"start\":\"{0}T{1}\",", eventItem.Start.ToString("yyyy-MM-dd"), eventItem.Start.ToString("HH:mm:ss"));
sb.AppendFormat("\"end\":\"{0}T{1}\",", eventItem.End.ToString("yyyy-MM-dd"), eventItem.End.ToString("HH:mm:ss"));
}
sb.AppendFormat("\"url\":\"{0}\"", eventItem.URL);
sb.Append("}");
i++;
if (i < count)
{
sb.Append(",");
}
}
sb.Append("]");
return sb.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment