public byte[] ICalSerializeToBytes(iCalendar iCal, string filename) { ISerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new SerializerFactory(); IStringSerializer serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer; string output = serializer.SerializeToString(iCal); return(Encoding.UTF8.GetBytes(output)); }
/// <summary> /// Creates calendar Objects and events /// </summary> /// <param name="model"></param> /// <returns>a MemoryStream</returns> private MemoryStream GetCalendarBookingBytes(CalendarInviteViewModel model) { var iCal = new Calendar(); var evt = iCal.Create <CalendarEvent>(); evt.Summary = model.Title; evt.Start = new CalDateTime(model.StartDate); evt.End = new CalDateTime(model.EndDate); evt.Description = model.Body; evt.Location = model.Address; if (model.StartDate.TimeOfDay.Hours == 0) { evt.IsAllDay = true; } evt.Uid = new Guid().ToString(); //evt.Organizer = new Organizer(organizer); evt.Alarms.Add(new Alarm { Duration = new TimeSpan(30, 0, 0), Trigger = new Trigger(new TimeSpan(30, 0, 0)), Action = AlarmAction.Display, Description = "Reminder" }); SerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new SerializerFactory(); var serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer; var output = serializer.SerializeToString(iCal); var bytes = Encoding.UTF8.GetBytes(output); MemoryStream ms = new MemoryStream(bytes); return(ms); }
public void AddPropertyShouldNotIncludePropertyNameInValue() { var propName = "X-WR-CALNAME"; var propValue = "Testname"; var iCal = new Calendar(); iCal.AddProperty(propName, propValue); var ctx = new SerializationContext(); var factory = new SerializerFactory(); // Get a serializer for our object var serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer; var result = serializer.SerializeToString(iCal); Console.WriteLine(result); var lines = result.Split(new [] { "\r\n" }, StringSplitOptions.None); var propLine = lines.FirstOrDefault(x => x.StartsWith("X-WR-CALNAME:")); Assert.AreEqual($"{propName}:{propValue}", propLine); }
public ActionResult ICalFeed(string email) { if (String.IsNullOrWhiteSpace(email)) { throw new Exception("Email missing."); } var password = ConfigurationManager.AppSettings[email]; if (String.IsNullOrWhiteSpace(password)) { throw new Exception(string.Format("User {0} does not exist.", email)); } var iCal = PlannedMoves.getTrainingPlanCal(email, password); ISerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new SerializerFactory(); var serializer = (IStringSerializer)factory.Build(iCal.GetType(), ctx); string output = serializer.SerializeToString(iCal); return(File(Encoding.UTF8.GetBytes(output), "text/calendar; charset=UTF-8", "Movescount.ical")); }