设为首页 收藏本站
查看: 733|回复: 0

[经验分享] Understanding the SharePoint calendar and how to export it to iCal format

[复制链接]

尚未签到

发表于 2015-9-29 09:44:11 | 显示全部楼层 |阅读模式
转载:http://blogs.msdn.com/sharepoint/archive/2007/05/14/understanding-the-sharepoint-calendar-and-how-to-export-it-to-ical-format.aspx

  Introduction
  One  of the challenges of accessing SharePoint calendars via the object model is that  there are so many different types of events – normal events, all-day events,  recurring events, recurrence exceptions, and deleted instances – and they all  look more or less the same! In this  post, I will shed some light on how to work with calendar events, describe the  subtleties of recurrences, and put that knowledge to work exporting events to  the RFC 2445 iCalendar  format.
  First,  before we can do anything with a Calendar list, we need a way to distinguish  between different event types.
Distinguishing  between calendar item types
  Calendar  items in SharePoint fall into several categories. Single events are those which don’t repeat  and only appear once on the calendar, while recurring events may show up any  number of times depending on the recurrence pattern selected by a user. Either type of event may have a defined start  and end time, or it may be an all-day event which lasts from midnight to 11:59  PM.
  Furthermore,  individual instances of a recurring event may be deleted or edited, creating a  new event which takes its place. The  event created by a deleted instance instructs the calendar not to render that  day’s instance of the recurring event.
  Calendar  events can be distinguished by looking at the fRecurrence, fAllDayEvent, and  EventType field values:
  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

Understanding  recurring events
  Recurring  events have a number of subtleties not found in single events. Most importantly, one recurring event item  expands into any number of recurring event instances when it is rendered in the  calendar view. Recurring events also  make use of fields left empty in single events.
  Certain  fields are interpreted differently depending on whether the item in question is  a recurring event or a single event:
  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.
  
  Similarly,  exceptions and deleted instances have fields which relate the event back to the  parent 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.
  
  Once  you’ve determined that you’re working with a recurring event, you’ll likely want  to work with individual instances of the recurrence.
Recurrence patterns  and expanding events
  Recurring  events store the pattern used to display instances of the recurring event as XML  in the RecurrenceData field. While this  data is best used in a read-only fashion, the following are examples of the  patterns you may encounter:
  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>
  
  Thankfully,  you don&#8217;t need to parse this XML yourself to get the actual instances of the  recurring event. Instead, you can use  the SharePoint object model to expand recurring events during a given  month:
  //  Get the Events list
  SPSite  site = new SPSite("http://localhost");
  SPWeb  web = site.RootWeb;
  SPList  calendarList = web.Lists["Calendar"];
  
  //  Construct a query that expands recurring events
  SPQuery  query = new SPQuery();
  query.ExpandRecurrence  = true;
  query.Query  = "<Where><DateRangesOverlap><FieldRef  Name=\"EventDate\" /><FieldRef Name=\"EndDate\" /><FieldRef  Name=\"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
  SPListItemCollection  calendarItems = calendarList.GetItems(query);
  
  foreach  (SPListItem item in calendarItems)
  {
  Console.WriteLine(item["Title"] + ": starts  "
  +  item["EventDate"].ToString() + " and ends "
  +  item["EndDate"].ToString());
  }
  Note  that after expansion, each instance of a recurring event has the same ID as the  recurring event that produced it. In  other words, a recurring event with ID 7 will appear as many recurring event  instances in the code above, each with an ID of 7.
Putting it to  work
  It&#8217;s  always easier to learn from an example than from dry technical descriptions, and  to that end I&#8217;ve developed a SharePoint Solution which uses the functionality  I&#8217;ve described above. When installed and  activated, it adds an &#8220;Export Calendar to iCal Format&#8221; button to the Actions  menu of every Calendar list in the site collection as pictured below.
DSC0000.jpg
  To  convert events to RFC 2445 iCalendar format, I loop through each event,  translating recurrence patterns into iCalendar format and associating exceptions  and deleted instances with the recurring event they came from. The majority of the code deals with  interpreting the recurrence pattern; however, there is also a significant amount  of code to translate SharePoint timezone information as  well.
  You  can download the iCalendar Exporter solution and source code, which have been  released as part of the Community Kit for SharePoint, right here. Launch Visual Studio, take a look at my  source code, and have fun developing custom solutions with the SharePoint  calendar! Please leave a comment if you have any questions or want to showcase  an innovative solution that you've built.
  
  Matt  Swann
SDET, SharePoint

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-120298-1-1.html 上篇帖子: SharePoint权限控制的问题(解答) 下篇帖子: 一旦出错伤不起的Office SharePoint WorkSpace 2010 !
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表