Free Support Forum - aspose.com

Create a monthly recurrence pattern that repeats every Nth weekday of the month

I am trying to use the Aspose.Email Java edition to create a monthly recurrence pattern that repeats every Nth weekday of the month. For example, say the event occurs on the 3rd Wednesday of the month, for 6 occurrences. I want to create a MapiCalendarMonthlyRecurrencePattern which expresses that.

Through some searching it seems I have to call .setPosition() and .setDayOfWeek() on the MapiCalendarMonthlyRecurrencePattern object. But when I do that, I get this error:

AsposeException: BYSETPOS must only be used in conjunction with another BYxxx rule part.
java.lang.StackTraceElement;
at com.aspose.email.RecurrenceRule.a(SourceFile:418)
at com.aspose.email.CalendarRecurrence.a(SourceFile:585)
at com.aspose.email.CalendarRecurrence.b(SourceFile:352)
at com.aspose.email.CalendarRecurrence.a(SourceFile:274)
at com.aspose.email.MapiCalendarRecurrencePatternFactory.a(SourceFile:468)

Here is a snippet that illustrates this:
Calendar jcalendar = Calendar.getInstance(java.util.TimeZone.getDefault());
jcalendar.set(2021, java.util.Calendar.MARCH, 17, 15, 30, 0);
Date startDate = jcalendar.getTime();
jcalendar.set(2021, java.util.Calendar.MARCH, 17, 16, 0, 0);
Date endDate = jcalendar.getTime();

  MapiCalendar event = new MapiCalendar("location", "summary", "description", startDate, endDate);
  MapiCalendarMonthlyRecurrencePattern pattern = new MapiCalendarMonthlyRecurrencePattern();
  pattern.setPatternType(MapiCalendarRecurrencePatternType.Month);
  pattern.setPeriod(1);
  pattern.setWeekStartDay(DayOfWeek.Sunday);
  pattern.setStartDate(startDate);

  pattern.setEndType(MapiCalendarRecurrenceEndType.EndAfterNOccurrences);
  pattern.setOccurrenceCount(6);
  pattern.setPosition(3);
  pattern.setDayOfWeek(DayOfWeek.Wednesday);

  MapiCalendarEventRecurrence r = new MapiCalendarEventRecurrence();
  r.setAppointmentTimeZoneDefinitionRecur(new MapiCalendarTimeZone("UTC"));
  r.setTimeZoneStruct(new MapiCalendarTimeZone("UTC"));
  r.setRecurrencePattern(pattern);
  r.setClipStart(startDate);
  event.setRecurrence(r);

  event.setStartDateTimeZone(new MapiCalendarTimeZone("UTC"));
  event.setEndDateTimeZone(new MapiCalendarTimeZone("UTC"));

  // writing the PST happens elsewhere in our code, the rest is copied from another forum post.
  PersonalStorage pst = PersonalStorage.create("DailyPst.pst", FileFormatVersion.Unicode);
  FolderInfo fi = pst.createPredefinedFolder("CalPst", StandardIpmFolder.Appointments);
  fi.addMapiMessageItem(event);

Is there something that must be changed to make this work?
Thanks for your help.

@cdeangelis,
Thank you for the issue description. It will take me a while to investigate it. I will inform you about any progress later.

@cdeangelis,
The exception doesn’t throw with Aspose.Email 21.2 on our end. But another exception is throwing. We will investigate both cases. I have logged the issue in our tracking system with ID EMAILJAVA-34848 to investigate further. I will inform you about any progress.
Please specify the version of Aspose.Email and JDK you are using.

@Andrey_Potapov
I am using: aspose-email-21.2.2-jdk16.jar

JDK: openjdk version “1.8.0_265”

More of the stack trace:
AsposeException: BYSETPOS must only be used in conjunction with another BYxxx rule part.
java.lang.StackTraceElement;
at com.aspose.email.RecurrenceRule.a(SourceFile:418)
at com.aspose.email.CalendarRecurrence.a(SourceFile:585)
at com.aspose.email.CalendarRecurrence.b(SourceFile:352)
at com.aspose.email.CalendarRecurrence.a(SourceFile:274)
at com.aspose.email.MapiCalendarRecurrencePatternFactory.a(SourceFile:468)
at com.aspose.email.MapiCalendar.a(SourceFile:797)
at com.aspose.email.MapiCalendar.a(SourceFile:744)
at com.aspose.email.FolderInfo.addMapiMessageItem(SourceFile:817)

@cdeangelis,
We investigated the issue. You should fix your code example as below:

//MapiCalendarRecurrencePatternType.MonthNth - The event has an every nth month pattern
pattern.setPatternType(MapiCalendarRecurrencePatternType.MonthNth); // <--
pattern.setPeriod(1);
pattern.setWeekStartDay(DayOfWeek.Sunday);
pattern.setStartDate(startDate);

pattern.setEndType(MapiCalendarRecurrenceEndType.EndAfterNOccurrences);
pattern.setOccurrenceCount(6);
pattern.setPosition(3);
// Flags attribute MapiCalendarDayOfWeek - days of week on which the event occurs
// For example:
//     pattern.setDayOfWeek(MapiCalendarDayOfWeek.Wednesday | MapiCalendarDayOfWeek.Thursday)
pattern.setDayOfWeek(MapiCalendarDayOfWeek.Wednesday); // <--

API Reference: MapiCalendarRecurrencePatternType Enumeration
More details: Working with Recurrences

@Andrey_Potapov,

Thank you very much for this information - I am able to generate the event as desired following this code snippet.

The description of the “MonthNth” pattern type could use improvement, it reads “The event has an every nth month pattern”. That makes it sound like the event would repeat at a specified interval of months (the period). Also, I did not see this type of event explained in the “Working with Recurrences” link, so that is an opportunity for enhancement.

Thanks for the fast response though, I appreciate it!

@cdeangelis,
You are right. We are constantly improving our documentation. Thank you for your remarks. We will take it into account.