Sitecore “Add To Calendar” Module

Whether you are building corporate website, commercial website or any other type of website, one of the famous feature demanding from your client is Calendar Event. There should be feature in your website from where user should be able to download calendar event (e.g. .ICS) file and save it as reminder. Such calendar event is required typically on news or event section of your website.

Clicking on Add To Calendar link will download calendar event – .ICS file as shown below.

If you are looking for similar thing, then you are at right place. Add To Calendar Sitecore module is all about this.

Features of Add To Calendar module:

1) Supports multiple time zones It will convert date and save calendar event based on end user’s timezone no matter in which time zone you have configured event.

For example: If you have configured event in US time zone and if end user is downloading that event from India, then it will convert event timings in IST. 2) Supports multiple occurrence of event in a week If you have multiple occurrence of event falling under one week, then this module will pick best suitable event for the user which means it will download very next upcoming occurrence of that event.

For example: If you have configured event for Monday, Wednesday & Friday and if the user downloads event on Tuesday then module is intelligent enough to understand very next event and will download Wednesday event for the user.

3) Calendar details are fully configurable You can manage event title, subject and body content straight away from Sitecore.

4) You can customize it if you want The code of Add To Calendar Sitecore module is available on GitHub, so you can customize or contribute it as per your wish at https://github.com/nileshthakkar/AddToCalendar

Download

You can download this module from Sitecore Market Place.

Configuration

Once you install it, you will find this module at /sitecore/system/Modules/Add To Calendar Second column in Event Dates field is for timings. You can specify any time range in 24 hours format.

The templates are at:

The controller can be found at:

So the only thing you need to do is add this controller to your page and you are good to go!

Make sure that you set the data source of Calendar Event item to AddToCalendar controller.

Source Code

In case if you are NOT using MVC and want to have this module working, then you can create a sublayout and inject below code on click of your “Add To Calendar” link.

 public void Download(Item calenderItem)         {                       DateTime chatStartDate = DateTime.Today;             DateTime chatEndDate = DateTime.Today;             string strTimings = string.Empty;             NameValueCollection collection =              Sitecore.Web.WebUtil.ParseUrlParameters(calenderItem["Event Dates"]);             if (collection.Count > 0)             {                 for (int i = 0; i <= 6; i++)                 {                     foreach (string day in collection.AllKeys)                     {                         if (chatStartDate.Date.AddDays(i).DayOfWeek.ToString() == day)                         {                             chatStartDate = chatStartDate.Date.AddDays(i);                             strTimings = collection[day];                             i = 7;                             break;                         }                     }                 }             }              string[] chatTimings =              strTimings.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries);             if (chatTimings.Length > 0)             {                 if (chatTimings[0] != null)                 {                     TimeSpan ts = new TimeSpan(int.Parse(chatTimings[0]), 0, 0);                     chatStartDate = chatStartDate.Date + ts;                 }                  if (chatTimings[1] != null)                 {                     TimeSpan ts2 = new TimeSpan(int.Parse(chatTimings[1]), 0, 0);                     chatEndDate = chatStartDate.Date + ts2;                 }                  if (calenderItem["Time Zone"] != null)                 {                     Item timeZoneItem = Sitecore.Context.Database.GetItem(calenderItem["Time Zone"]);                      chatStartDate =                      convertDateToTimeZone(chatStartDate, timeZoneItem["Name"], "UTC");                     chatEndDate =                     convertDateToTimeZone(chatEndDate, timeZoneItem["Name"], "UTC");                      CalendarFile calendarFile = new CalendarFile(calenderItem["Subject"], chatStartDate, chatEndDate);                     calendarFile.location = calenderItem["Location"];                     calendarFile.description = calenderItem["Body"];                      this.Response.Clear();                     Response.Expires = 0;                     this.Response.Buffer = true;                     Response.ContentType = "text/calendar";                     Response.AddHeader("Content-Disposition", "inline; filename=" + calenderItem.Name + ".ics");                     this.Response.Write(calendarFile.ToString());                     this.Response.Flush();                     this.Response.End();                 }             }         }          public DateTime convertDateToTimeZone(DateTime nextDate, string fromTimeZone, string toTimeZone)         {             TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById(fromTimeZone);             TimeZoneInfo cstZone2 = TimeZoneInfo.FindSystemTimeZoneById(toTimeZone);             nextDate = DateTime.SpecifyKind(nextDate, DateTimeKind.Unspecified);             DateTime cstDateTime = TimeZoneInfo.ConvertTime(nextDate, cstZone, cstZone2);             return cstDateTime;         }  

Feel free to customize or contribute this module by forking it at: https://github.com/nileshthakkar/AddToCalendar Hope this effort will help someone to add Calendar Event feature in his/her project!

Leave a Reply