首先要搞清楚日历事件的各种类型,参考文章:
Type | Description | fRecurrence | fAllDayEvent | EventType |
Single event |
An event created with the All Day Event and Recurrence checkboxes unselected. |
FALSE | FALSE | 0 |
All-day event |
An event created with the All Day Event checkbox selected. |
FALSE | TRUE | 0 |
Recurring event |
An event created with the Recurrence checkbox selected. Has a recurrence icon in the All Events view. Appears as a single master event on the All Events view, but as recurring instances on the Current Events and Calendar views. |
TRUE | FALSE | 1 |
Recurring all-day event |
Same as above, but with the All Day Event checkbox selected at creation time. |
TRUE | TRUE | 1 |
Recurrence exception |
Created by editing an instance of a recurring event. Has a strikethrough recurrence icon in the All Events view. |
TRUE | FALSE | 4 |
All-day recurrence exception |
Same as above, but created by editing an instance of an all-day recurring event. |
TRUE | TRUE | 4 |
Deleted instance of a recurring event |
Created by deleting a instance of a recurring event. Title is prefixed with “Deleted:” in the All Events view, and is hidden in the Current Events and Calendar views. |
TRUE | FALSE | 3 |
Deleted instance of an all-day recurring event |
Same as above, but created by deleting an instance of an all-day recurring event. |
TRUE | TRUE | 3 |
重复事件
Field | Value for single event | Value for recurring event |
EventDate | Start date and time |
Start date and time set for the recurring event when it was created, which may be an earlier date than the first instance of the recurring event. |
EndDate | End date and time |
End date and time for the last instance of the recurring event. For recurring events with no end date, this is a computed date several years in the future. |
Duration |
The time in seconds between EventDate and EndDate. |
The duration in seconds of an individual instance of the recurring event. |
特例事件和已删除事件
Field | Value for exception or deleted instance |
MasterSeriesItemID |
The ID of the recurring event from which this exception or deleted instance was made. |
RecurrenceID |
The date and time of the instance of the recurring event which this exception or deleted instance takes the place of. |
重复事件的重复模式
Recurrence type | RecurrenceData field value |
Daily every 1 days, no end date |
<recurrence> <rule> <firstDayOfWeek>su</firstDayOfWeek> <repeat><daily dayFrequency="1" /></repeat> <repeatForever>FALSE</repeatForever> </rule> </recurrence> |
Weekly every Monday, Tuesday, and Wednesday, end by 5/31/2007 |
<recurrence> <rule> <firstDayOfWeek>su</firstDayOfWeek> <repeat> <weekly mo="TRUE" tu="TRUE" we="TRUE" weekFrequency="1" /> </repeat> <windowEnd>2007-05-31T22:00:00Z</windowEnd> </rule> </recurrence> |
Monthly the third Wednesday of every 2 months, no end date |
<recurrence> <rule> <firstDayOfWeek>su</firstDayOfWeek> <repeat> <monthlyByDay we="TRUE" weekdayOfMonth="third" monthFrequency="2" /> </repeat> <repeatForever>FALSE</repeatForever> </rule> </recurrence> |
Yearly every May 18, end after 10 instances |
<recurrence> <rule> <firstDayOfWeek>su</firstDayOfWeek> <repeat><yearly yearFrequency="1" month="5" day="18" /></repeat> <repeatInstances>10</repeatInstances> </rule> </recurrence> |
虽然事件的重复模式是以XML格式储存,但在使用ClientObjectModel查询时,并不需要手动去解析XML。
使用SPQuery对象查询时,需指定以下属性:
ExpandRecurrence -- 是否展开重复事件
CalendarDate -- 指定查询的参考时间
查询每日事件
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList calendar = web.GetList(listUrl);
SPQuery caml = new SPQuery();
caml.Query = @"<Where>
<DateRangesOverlap>
<FieldRef Name='EventDate' />;
<FieldRef Name='EndDate' />
<FieldRef Name='RecurrenceID' />
<Value Type='DateTime'><Today /></Value>
</DateRangesOverlap>
</Where>"; caml.ExpandRecurrence = true;
caml.CalendarDate = DateTime.Now; return calendar.GetItems(caml);
}
}
查询每周事件
caml.Query = @"<Where>
<DateRangesOverlap>
<FieldRef Name='EventDate' />;
<FieldRef Name='EndDate' />
<FieldRef Name='RecurrenceID' />
<Value Type='DateTime'><Week /></Value>
</DateRangesOverlap>
</Where>";
查询每月事件
caml.Query = @"<Where>
<DateRangesOverlap>
<FieldRef Name='EventDate' />;
<FieldRef Name='EndDate' />
<FieldRef Name='RecurrenceID' />
<Value Type='DateTime'><Month /></Value>
</DateRangesOverlap>
</Where>";
查询每年事件
caml.Query = @"<Where>
<DateRangesOverlap>
<FieldRef Name='EventDate' />;
<FieldRef Name='EndDate' />
<FieldRef Name='RecurrenceID' />
<Value Type='DateTime'><Year /></Value>
</DateRangesOverlap>
</Where>";