3

I have done the following code to save a recurring entry to Calendar List named Bookings.

    SPSite site = new SPSite(SPContext.Current.Web.Url);
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["Bookings"];
        SPListItem recEvent = list.Items.Add();
        try
        {
            recEvent["EventDate"] = startdate;//11/09/2012 08:00:00
            recEvent["EndDate"] = enddate;//11/09/2012 08:30:00
            recEvent["Booking_x0020_Title"] = Convert.ToString(txtTitle.Text);
            recEvent["Title"] = Convert.ToString(txtTitle.Text);
            recEvent["RecurrenceData"] = Recur_Xml.ToString();//ReccurrenceRule
            recEvent["fRecurrence"] = "True";
            recEvent["EventType"] = 1;
            recEvent["UID"] = System.Guid.NewGuid();
            recEvent["TimeZone"] = 0;
            recEvent["Recurrence"] = -1;
            recEvent["XMLTZone"] = "<timeZoneRule><standardBias>-330</standardBias><additionalDaylightBias>-60</additionalDaylightBias></timeZoneRule>";

            recEvent.Update();
        }
        catch (Exception er)
        {

        }

If i save the above with Recurrence rule as Repeat every 1 day for 10 occurences than the Item is saved properly and Current Events view shows all the instances but while fetching the Recurring entry i get only 1 Result i.e on Start Date. The following is the Code to retrieve recurrent values:

SPWeb oWeb = SPContext.Current.Web;
                SPList oListBookings = oWeb.Lists.TryGetList("Bookings");
SPQuery oQueryBookings = new SPQuery();

                            string strQuery = "";
    strQuery = @"<Where><DateRangesOverlap><FieldRef Name='EventDate' /><FieldRef Name='EndDate' /><FieldRef Name='RecurrenceID' /><Value Type='DateTime'><Today /></Value></DateRangesOverlap></Where>";
                            oQueryBookings.ExpandRecurrence = true;
                            oQueryBookings.Query = strQuery;
                            oQueryBookings.CalendarDate = date;
    SPListItemCollection oCollBookings = oListBookings.GetItems(oQueryBookings);

The result is shown only if oQueryBookings.CalendarDate = date; equals 11/09/2012 08:00:00 and it does not return any items for Dates after that even when the instances are present for these dates till 19/09/2012 08:00:00

The query returns all the item if i set the recEvent["EndDate"] to 19/09/2012 08:00:00 while saving the item. I want the EndDate for recurring items to be set to same day on user form (no user would want to calculate the last date of his occurence for recurring pattern with no end date) and it should be autocalculated on save.How is that possible?

If I make the entry using sharepoint UI than this End date is autocalculated even if it is set to the same day and if its a recurring entry.

Please guide where am I going wrong? Is it while saving or while fetching the records? Or is it because the EndDate is the same date as StartDate? Kindly help.

tanascius
  • 53,078
  • 22
  • 114
  • 136
Ishan
  • 4,008
  • 32
  • 90
  • 153

1 Answers1

0

Check "fRecurrence" and "Recurrence" fields. You can try to use "1" instead of "True" and I am not sure about "Recurrence" == -1. Could you please clarify what does it mean?

One more idea. You can create recurred event, using SharePoint Site, to test your second part of code.

As I know for recurring events with no end date, "EndDate" is a computed date several years in the future. So if your code is correct, to handle recurrence events, you should update "EndDate" by appending additional years.

Warlock
  • 7,321
  • 10
  • 55
  • 75
  • Thanks. `EndDate` for an event which occurs after every 2 months only on sunday and end after 10 occurences! how will i get the enddate of such an event? My issue is not with the events with no end date, i can append many years in future and it works fine. My issue is only with events repeating for certain instances. – Ishan Oct 29 '12 at 09:12
  • Ok. For repeating instances you should check a RecurrenceData. The xml value should contain a "repeatinstances" tag with a number of instances. For example, if you want that event would be ended after 10 occurrences, the xml value should be like this: su 10 Please check attributes in the "monthlyByDay" tag. – Warlock Oct 29 '12 at 15:49