public void ProcessRequest(HttpContext context) { var host = context.Request.Url.Host; // initialize calendar item var cal = new iCalendar(); cal.Version = "1.0"; // find the event Telerik.Sitefinity.Events.Model.Event ev; using (var fluent = App.WorkWith()) { // TODO: replace this with correct logic for retrieving event ev = fluent.Events().Publihed().Get().FirstOrDefault(); } if (ev == null) return; var appt = new DDay.iCal.Event(); appt.Start = new iCalDateTime(ev.EventStart.ToUniversalTime()); if (ev.EventEnd.HasValue) appt.End = new iCalDateTime(ev.EventEnd.Value.ToUniversalTime()); else appt.IsAllDay = true; // save and format description var description = ev.Content.ToString().Replace("\r\n", "<br /><br/>"); description = description.Replace("src=\"/", "src=\"http://" + host + "/"); appt.AddProperty("X-ALT-DESC", description); // non-html property var reg = new Regex("<(.|\n)+?>"); appt.Description = reg.Replace(description, ""); // event location var location = ev.Street; // location = location == null ? ev.ContentItem.GetMetaData("Street") : string.Concat(location, " (", ev.ContentItem.GetMetaData("Street"), ")"); appt.Location = location.ToString(); appt.Summary = ev.Title; // url //var evUrl = ConfigurationManager.AppSettings[EventsManager.DefaultContentProvider + "Url"]; //if (string.IsNullOrEmpty(evUrl)) break; appt.Url = new Uri("http://www.bing.com");// string.Concat("http://", host, evUrl, ev.ContentItem.UrlWithExtension); cal.Events.Add(appt); // write calendar feed! var ser = new iCalendarSerializer(cal); context.Response.Clear(); context.Response.ContentType = "text/calendar"; context.Response.AddHeader("content-disposition", "attachment; filename=Calendar.ics"); context.Response.Write(ser.SerializeToString()); //context.Response.Flush(); }
/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface. /// </summary> /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> public void ProcessRequest(HttpContext context) { var host = context.Request.Url.Host; // TODO: Support Filtering by date/month // TODO: Support filtering by category // initializie calendar var cal = new iCalendar(); cal.Version = "1.0"; // get events +/- 6 months IQueryable events; using (var fluent = App.WorkWith()) { var center = new DateTime(year, month, 1); var start = center.AddMonths(-6); var end = center.AddMonths(6); events = fluent.Events().Where(e => e.EventStart >= start && e.EventEnd <= end && e.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).Get(); } foreach (Telerik.Sitefinity.Events.Model.Event ev in events) { var appt = new DDay.iCal.Event(); appt.Start = new iCalDateTime(ev.EventStart.ToUniversalTime()); if (ev.EventEnd.HasValue) { appt.End = new iCalDateTime(ev.EventEnd.Value.ToUniversalTime()); } else { appt.IsAllDay = true; } // save and format description var description = ev.Content.ToString().Replace("\r\n", "<br /><br/>"); description = description.Replace("src=\"/", "src=\"http://" + host + "/"); appt.AddProperty("X-ALT-DESC", description); // non-html property var reg = new Regex("<(.|\n)+?>"); appt.Description = reg.Replace(description, ""); // event location var location = ev.Street; // location = location == null ? ev.ContentItem.GetMetaData("Street") : string.Concat(location, " (", ev.ContentItem.GetMetaData("Street"), ")"); appt.Location = location.ToString(); appt.Summary = ev.Title; // url //var evUrl = ConfigurationManager.AppSettings[EventsManager.DefaultContentProvider + "Url"]; //if (string.IsNullOrEmpty(evUrl)) break; appt.Url = new Uri("http://www.bing.com"); // string.Concat("http://", host, evUrl, ev.ContentItem.UrlWithExtension); cal.Events.Add(appt); } // write calendar feed! var ser = new iCalendarSerializer(cal); context.Response.Clear(); context.Response.ContentType = "text/calendar"; context.Response.AddHeader("content-disposition", "attachment; filename=Calendar.ics"); context.Response.Write(ser.SerializeToString()); //context.Response.Flush(); }
/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface. /// </summary> /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> public void ProcessRequest(HttpContext context) { var host = context.Request.Url.Host; // TODO: Support Filtering by date/month // TODO: Support filtering by category // initializie calendar var cal = new iCalendar(); cal.Version = "1.0"; // get events +/- 6 months IQueryable events; using (var fluent = App.WorkWith()) { var center = new DateTime(year, month, 1); var start = center.AddMonths(-6); var end = center.AddMonths(6); events = fluent.Events().Where(e => e.EventStart >= start && e.EventEnd <= end && e.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).Get(); } foreach (Telerik.Sitefinity.Events.Model.Event ev in events) { var appt = new DDay.iCal.Event(); appt.Start = new iCalDateTime(ev.EventStart.ToUniversalTime()); if (ev.EventEnd.HasValue) appt.End = new iCalDateTime(ev.EventEnd.Value.ToUniversalTime()); else appt.IsAllDay = true; // save and format description var description = ev.Content.ToString().Replace("\r\n", "<br /><br/>"); description = description.Replace("src=\"/", "src=\"http://" + host + "/"); appt.AddProperty("X-ALT-DESC", description); // non-html property var reg = new Regex("<(.|\n)+?>"); appt.Description = reg.Replace(description, ""); // event location var location = ev.Street; // location = location == null ? ev.ContentItem.GetMetaData("Street") : string.Concat(location, " (", ev.ContentItem.GetMetaData("Street"), ")"); appt.Location = location.ToString(); appt.Summary = ev.Title; // url //var evUrl = ConfigurationManager.AppSettings[EventsManager.DefaultContentProvider + "Url"]; //if (string.IsNullOrEmpty(evUrl)) break; appt.Url = new Uri("http://www.bing.com");// string.Concat("http://", host, evUrl, ev.ContentItem.UrlWithExtension); cal.Events.Add(appt); } // write calendar feed! var ser = new iCalendarSerializer(cal); context.Response.Clear(); context.Response.ContentType = "text/calendar"; context.Response.AddHeader("content-disposition", "attachment; filename=Calendar.ics"); context.Response.Write(ser.SerializeToString()); //context.Response.Flush(); }
public void ProcessRequest(HttpContext context) { var host = context.Request.Url.Host; // initialize calendar item var cal = new iCalendar(); cal.Version = "1.0"; // find the event Telerik.Sitefinity.Events.Model.Event ev; using (var fluent = App.WorkWith()) { // TODO: replace this with correct logic for retrieving event ev = fluent.Events().Publihed().Get().FirstOrDefault(); } if (ev == null) { return; } var appt = new DDay.iCal.Event(); appt.Start = new iCalDateTime(ev.EventStart.ToUniversalTime()); if (ev.EventEnd.HasValue) { appt.End = new iCalDateTime(ev.EventEnd.Value.ToUniversalTime()); } else { appt.IsAllDay = true; } // save and format description var description = ev.Content.ToString().Replace("\r\n", "<br /><br/>"); description = description.Replace("src=\"/", "src=\"http://" + host + "/"); appt.AddProperty("X-ALT-DESC", description); // non-html property var reg = new Regex("<(.|\n)+?>"); appt.Description = reg.Replace(description, ""); // event location var location = ev.Street; appt.Location = location.ToString(); appt.Summary = ev.Title; // url appt.Url = new Uri("http://www.bing.com"); cal.Events.Add(appt); // write calendar feed! var ser = new iCalendarSerializer(cal); context.Response.Clear(); context.Response.ContentType = "text/calendar"; context.Response.AddHeader("content-disposition", "attachment; filename=Calendar.ics"); context.Response.Write(ser.SerializeToString()); }
/// <summary> /// Creates the iCalendar object and populates it with events /// </summary> /// <param name="calendarProps">The calendar props.</param> /// <returns></returns> private iCalendar CreateICalendar(CalendarProps calendarProps, string interactionDeviceType) { // Get a list of confirmed attendances filtered by calendarProps List <Attendance> attendances = GetAttendances(calendarProps); // Create the iCalendar iCalendar icalendar = new iCalendar(); icalendar.AddLocalTimeZone(); TimeSpan duration = TimeSpan.MinValue; int currentScheduleId = -1; // Create each of the attendances foreach (var attendance in attendances) { using (var rockContext = new RockContext()) { var attendanceOccurrenceService = new AttendanceOccurrenceService(rockContext); var schedule = attendanceOccurrenceService .Queryable() .AsNoTracking() .Where(a => a.Id == attendance.OccurrenceId) .Select(a => a.Schedule) .FirstOrDefault(); string scheduleName = schedule.Name; // TODO: Construct a description that includes the group leader contact info and the URL to the schedule toolbox. string description = schedule.Description; string locationName = attendanceOccurrenceService .Queryable() .AsNoTracking() .Where(a => a.Id == attendance.OccurrenceId) .Select(a => a.Location.Name) .FirstOrDefault() ?? string.Empty; if (schedule.Id != currentScheduleId) { // We have to get the duration from Schedule.iCal for this attendance. // Attendances are ordered by scheduleId so this only happens once for each unique schedule. iCalendarSerializer serializer = new iCalendarSerializer(); iCalendarCollection ical = ( iCalendarCollection )serializer.Deserialize(schedule.iCalendarContent.ToStreamReader()); duration = ical[0].Events[0].Duration; currentScheduleId = schedule.Id; } var iCalEvent = new DDay.iCal.Event(); iCalEvent.Summary = scheduleName; iCalEvent.Location = locationName; iCalEvent.DTStart = new DDay.iCal.iCalDateTime(attendance.StartDateTime); iCalEvent.DTStart.SetTimeZone(icalendar.TimeZones[0]); iCalEvent.Duration = duration; // Don't set the description prop for outlook to force it to use the X-ALT-DESC property which can have markup. if (interactionDeviceType != "Outlook") { iCalEvent.Description = description.ConvertBrToCrLf().SanitizeHtml(); } // HTML version of the description for outlook iCalEvent.AddProperty("X-ALT-DESC;FMTTYPE=text/html", "<html>" + description + "</html>"); // classification: "PUBLIC", "PRIVATE", "CONFIDENTIAL" iCalEvent.Class = "PUBLIC"; //Add contact info for the group leader int groupId = attendanceOccurrenceService .Queryable() .AsNoTracking() .Where(a => a.Id == attendance.OccurrenceId) .Select(a => a.GroupId) .FirstOrDefault() ?? -1; Person groupLeader = new GroupMemberService(rockContext).GetLeaders(groupId).AsNoTracking().Select(m => m.Person).FirstOrDefault() ?? null; if (groupLeader != null) { iCalEvent.Organizer = new Organizer(string.Format("MAILTO:{0}", groupLeader.Email)); iCalEvent.Organizer.CommonName = groupLeader.FullName; // Outlook doesn't seem to use Contacts or Comments string contactName = !string.IsNullOrEmpty(groupLeader.FullName) ? "Name: " + groupLeader.FullName : string.Empty; string contactEmail = !string.IsNullOrEmpty(groupLeader.Email) ? ", Email: " + groupLeader.Email : string.Empty; string contactInfo = contactName + contactEmail; iCalEvent.Contacts.Add(contactInfo); iCalEvent.Comments.Add(contactInfo); } icalendar.Events.Add(iCalEvent); } } return(icalendar); }