fjptec-xm 发表于 2015-9-29 09:44:11

Understanding the SharePoint calendar and how to export it to iCal format

转载:http://blogs.msdn.com/sharepoint/archive/2007/05/14/understanding-the-sharepoint-calendar-and-how-to-export-it-to-ical-format.aspx

  Introduction
  Oneof the challenges of accessing SharePoint calendars via the object model is thatthere are so many different types of events – normal events, all-day events,recurring events, recurrence exceptions, and deleted instances – and they alllook more or less the same! In thispost, I will shed some light on how to work with calendar events, describe thesubtleties of recurrences, and put that knowledge to work exporting events tothe RFC 2445 iCalendarformat.
  First,before we can do anything with a Calendar list, we need a way to distinguishbetween different event types.
Distinguishingbetween calendar item types
  Calendaritems in SharePoint fall into several categories. Single events are those which don’t repeatand only appear once on the calendar, while recurring events may show up anynumber of times depending on the recurrence pattern selected by a user. Either type of event may have a defined startand end time, or it may be an all-day event which lasts from midnight to 11:59PM.
  Furthermore,individual instances of a recurring event may be deleted or edited, creating anew event which takes its place. Theevent created by a deleted instance instructs the calendar not to render thatday’s instance of the recurring event.
  Calendarevents can be distinguished by looking at the fRecurrence, fAllDayEvent, andEventType field values:
  Type
  Description
  fRecurrence
  fAllDayEvent
  EventType
  Singleevent
  An event created withthe All Day Event and Recurrence checkboxesunselected.
False
False
0
  All-dayevent
  An event created withthe All Day Event checkbox selected.
False
True
0
  Recurring event
  An event created withthe Recurrence checkbox selected. Has arecurrence icon in the All Events view. Appears as a single master event on the All Events view, but as recurringinstances on the Current Events and Calendar views.
True
False
1
  Recurring all-dayevent
  Same as above, butwith the All Day Event checkbox selected at creationtime.
True
True
1
  Recurrenceexception
  Created by editing aninstance of a recurring event. Has astrikethrough recurrence icon in the All Events view.
True
False
4
  All-day recurrenceexception
  Same as above, butcreated by editing an instance of an all-day recurringevent.
True
True
4
  Deleted instance of arecurring event
  Created by deleting ainstance of a recurring event. Title isprefixed with “Deleted:” in the All Events view, and is hidden in the CurrentEvents and Calendar views.
True
False
3
  Deleted instance ofan all-day recurring event
  Same as above, butcreated by deleting an instance of an all-day recurringevent.
True
True
3
Understandingrecurring events
  Recurringevents have a number of subtleties not found in single events. Most importantly, one recurring event itemexpands into any number of recurring event instances when it is rendered in thecalendar view. Recurring events alsomake use of fields left empty in single events.
  Certainfields are interpreted differently depending on whether the item in question isa recurring event or a single event:
  Field
  Valuefor single event
  Valuefor recurring event
  EventDate
  Start date andtime
  Start date and timeset for the recurring event when it was created, which may be an earlier datethan the first instance of the recurring event.
  EndDate
  End date andtime
  End date and time forthe last instance of the recurring event. For recurring events with no end date, this is a computed date severalyears in the future.
  Duration
  The time in secondsbetween EventDate and EndDate.
  The duration inseconds of an individual instance of the recurringevent.
  
  Similarly,exceptions and deleted instances have fields which relate the event back to theparent recurring event:
  Field
  Valuefor exception or deleted instance
  MasterSeriesItemID
  The ID of therecurring event from which this exception or deleted instance wasmade.
  RecurrenceID
  The date and time ofthe instance of the recurring event which this exception or deleted instancetakes the place of.
  
  Onceyou’ve determined that you’re working with a recurring event, you’ll likely wantto work with individual instances of the recurrence.
Recurrence patternsand expanding events
  Recurringevents store the pattern used to display instances of the recurring event as XMLin the RecurrenceData field. While thisdata is best used in a read-only fashion, the following are examples of thepatterns you may encounter:
  Recurrencetype
  RecurrenceDatafield value
  Dailyevery 1 days, no end date
  <recurrence><rule>
  <firstDayOfWeek>su</firstDayOfWeek>
  <repeat><dailydayFrequency="1" /></repeat>
  <repeatForever>FALSE</repeatForever>
  </rule></recurrence>
  Weeklyevery Monday, Tuesday, and Wednesday, end by5/31/2007
  <recurrence><rule>
  <firstDayOfWeek>su</firstDayOfWeek>
  <repeat><weeklymo="TRUE" tu="TRUE" we="TRUE" weekFrequency="1"/></repeat>
  <windowEnd>2007-05-31T22:00:00Z</windowEnd>
  </rule></recurrence>
  Monthlythe third Wednesday of every 2 months, no end date
  <recurrence><rule>
  <firstDayOfWeek>su</firstDayOfWeek>
  <repeat><monthlyByDaywe="TRUE" weekdayOfMonth="third" monthFrequency="2"/></repeat>
  <repeatForever>FALSE</repeatForever>
  </rule></recurrence>
  Yearlyevery May 18, end after 10 instances
  <recurrence><rule>
  <firstDayOfWeek>su</firstDayOfWeek>
  <repeat><yearlyyearFrequency="1" month="5" day="18" /></repeat>
  <repeatInstances>10</repeatInstances>
  </rule></recurrence>
  
  Thankfully,you don’t need to parse this XML yourself to get the actual instances of therecurring event. Instead, you can usethe SharePoint object model to expand recurring events during a givenmonth:
  //Get the Events list
  SPSitesite = new SPSite("http://localhost");
  SPWebweb = site.RootWeb;
  SPListcalendarList = web.Lists["Calendar"];
  
  //Construct a query that expands recurring events
  SPQueryquery = new SPQuery();
  query.ExpandRecurrence= true;
  query.Query= "<Where><DateRangesOverlap><FieldRefName=\"EventDate\" /><FieldRef Name=\"EndDate\" /><FieldRefName=\"RecurrenceID\" /><Value Type=\"DateTime\"><Month/></Value></DateRangesOverlap></Where>";
  
  //Look forward from the beginning of the current month
  query.CalendarDate= new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  
  //Returns all items (including recurrence instances) that
  //would appear in the calendar view for the current month
  SPListItemCollectioncalendarItems = calendarList.GetItems(query);
  
  foreach(SPListItem item in calendarItems)
  {
  Console.WriteLine(item["Title"] + ": starts"
  +item["EventDate"].ToString() + " and ends "
  +item["EndDate"].ToString());
  }
  Notethat after expansion, each instance of a recurring event has the same ID as therecurring event that produced it. Inother words, a recurring event with ID 7 will appear as many recurring eventinstances in the code above, each with an ID of 7.
Putting it towork
  It’salways easier to learn from an example than from dry technical descriptions, andto that end I’ve developed a SharePoint Solution which uses the functionalityI’ve described above. When installed andactivated, it adds an “Export Calendar to iCal Format” button to the Actionsmenu of every Calendar list in the site collection as pictured below.

  Toconvert events to RFC 2445 iCalendar format, I loop through each event,translating recurrence patterns into iCalendar format and associating exceptionsand deleted instances with the recurring event they came from. The majority of the code deals withinterpreting the recurrence pattern; however, there is also a significant amountof code to translate SharePoint timezone information aswell.
  Youcan download the iCalendar Exporter solution and source code, which have beenreleased as part of the Community Kit for SharePoint, right here. Launch Visual Studio, take a look at mysource code, and have fun developing custom solutions with the SharePointcalendar! Please leave a comment if you have any questions or want to showcasean innovative solution that you've built.
  
  MattSwann
SDET, SharePoint
页: [1]
查看完整版本: Understanding the SharePoint calendar and how to export it to iCal format