如何:向多个网站上的列表添加一个定期事件

上次修改时间: 2015年3月9日

适用范围: SharePoint Foundation 2010

在 SharePoint Online 中提供

此编程任务演示如何将具有会议工作区网站的定期事件添加到网站集中每个网站的 Calendar 列表。

将具有会议工作区网站的定期事件添加到网站集中每个网站的 Calendar 列表

  1. 在 Microsoft Visual Studio 2010 中创建控制台应用程序,如如何:创建控制台应用程序中所述。

  2. Microsoft.SharePointMicrosoft.SharePoint.Meetings 命名空间将 using 或 Imports 指令添加到 .cs 或 .vb 文件的开头,如下所示。

    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.Meetings
    
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Meetings;
    
  3. 使用 SPSite 构造函数来实例化指定的网站集。该示例使用 SPSite 类的 AllWebs 属性返回网站集中的所有网站。该示例使用 foreach 循环枚举网站,如果通过标准 SharePoint Foundation 模板 (WebTemplateSTS) 创建网站,则该示例获取网站的 Calendar 列表以及列表中列表项的集合。

    Dim evtTitle As String = Console.ReadLine()
    
    Using siteCollection As New SPSite("https://Server/sites/SiteCollection")
        Dim collWeb As SPWebCollection = siteCollection.AllWebs
    
        For Each oWebsite As SPWeb In collWeb
            If oWebsite.WebTemplate = SPWebTemplate.WebTemplateSTS Then
                Dim list As SPList = oWebsite.Lists("Calendar")
                Dim listItems As SPListItemCollection = list.Items
    
    string evtTitle = Console.ReadLine();
    
    using (SPSite siteCollection = new SPSite("https://Server/sites/SiteCollection"))
    {
        SPWebCollection collWeb = siteCollection.AllWebs;
    
        foreach (SPWeb oWebsite in collWeb)
        {
            if (oWebsite.WebTemplate == SPWebTemplate.WebTemplateSTS)
            {
                SPList list = oWebsite.Lists["Calendar"];
                SPListItemCollection listItems = list.Items;
    
  4. 若要创建列表项,请使用 SPListItemCollection 类的 Add 方法创建未初始化的列表项,使用索引器设置新项的各种属性,然后使用 Update 方法完成对项的创建。

    Dim recEvent As SPListItem = listItems.Add()
    
    Dim recdata As String = "<recurrence><rule>" &
        "<firstDayOfWeek>su</firstDayOfWeek>" & 
        "<repeat><daily dayFrequency='1' /></repeat>" & 
        "<repeatInstances>5</repeatInstances></rule></recurrence>"
    
    recEvent("Title") = evtTitle
    recEvent("RecurrenceData") = recdata
    recEvent("EventType") = 1
    recEvent("EventDate") = New DateTime(2011, 8, 15, 8, 0, 0)
    recEvent("EndDate") = New DateTime(2011, 9, 25, 9, 0, 0)
    recEvent("UID") = System.Guid.NewGuid()
    recEvent("TimeZone") = 13
    recEvent("Recurrence") = - 1
    recEvent("XMLTZone") = "<timeZoneRule>" &
        "<standardBias>480</standardBias>" &
        "<additionalDaylightBias>-60</additionalDaylightBias>" &
        "<standardDate><transitionRule  month='10' day='su' weekdayOfMonth='last' />" &
        "<transitionTime>2:0:0</transitionTime></standardDate>" &
        "<daylightDate><transitionRule  month='4' day='su' weekdayOfMonth='first' />" &
        "<transitionTime>2:0:0</transitionTime>" &
        "</daylightDate></timeZoneRule>"
    
    recEvent.Update()
    
    SPListItem recEvent = listItems.Add();
    
    string recData = "<recurrence><rule>" + 
        "<firstDayOfWeek>su</firstDayOfWeek>" +
        "<repeat><daily dayFrequency='1' /></repeat>" +
        "<repeatInstances>5</repeatInstances></rule></recurrence>";
    
    recEvent["Title"] = evtTitle;
    recEvent["RecurrenceData"] = recData;
    recEvent["EventType"] = 1;
    recEvent["EventDate"] = new DateTime(2011,8,15,8,0,0);
    recEvent["EndDate"] = new DateTime(2011,9,25,9,0,0);
    recEvent["UID"] = System.Guid.NewGuid();
    recEvent["TimeZone"] = 13;
    recEvent["Recurrence"] = -1;
    recEvent["XMLTZone"] = "<timeZoneRule>" +
        "<standardBias>480</standardBias>" +
        "<additionalDaylightBias>-60</additionalDaylightBias>" +
        "<standardDate><transitionRule  month='10' day='su' weekdayOfMonth='last' />" +
        "<transitionTime>2:0:0</transitionTime></standardDate>" +
        "<daylightDate><transitionRule  month='4' day='su' weekdayOfMonth='first' />" +
        "<transitionTime>2:0:0</transitionTime>" +
        "</daylightDate></timeZoneRule>";
    
        recEvent.Update();
    

    recData 变量包含一个 XML 片段,该片段指定每五天发生一次的定期事件的属性,并且 XMLTZone 索引器分配当前网站的时区信息。定义重复周期并指定时区信息的 XML 包含在内容数据库中的 UserData 表的列 ntext3 和 ntext4 中。

    下表演示可使用的不同类型重复周期的示例。

    说明

    示例

    每隔一天,直到指定的结束日期

    <recurrence><rule>
       <firstDayOfWeek>su</firstDayOfWeek>
       <repeat><daily dayFrequency='2' 
          /></repeat>
       <windowEnd>2003-09-
          20T09:00:00Z</windowEnd>
    </rule></recurrence>

    每个星期一

    <recurrence><rule>
       <firstDayOfWeek>su</firstDayOfWeek>
       <repeat><weekly mo='TRUE' 
          weekFrequency='1' /></repeat>
       <repeatForever>FALSE</repeatForever>
    </rule></recurrence>

    每两个月的第三天,总共五次

    <recurrence><rule>
       <firstDayOfWeek>su</firstDayOfWeek>
       <repeat><monthly monthFrequency='2'
          day='3' /></repeat>
       <repeatInstances>5</repeatInstances>
    </rule></recurrence>

    每月的第一个星期二,直到指定的结束日期

    <recurrence><rule>
       <firstDayOfWeek>su</firstDayOfWeek>
       <repeat>
          <monthlyByDay tu='TRUE' 
             weekdayOfMonth='first' 
             monthFrequency='1' />
       </repeat>
       <windowEnd>2003-08-
          02T10:00:00Z</windowEnd>
    </rule></recurrence>

    每年的第九个月的第二十天,直到指定的结束日期

    <recurrence><rule>
       <firstDayOfWeek>su</firstDayOfWeek>
       <repeat><yearly yearFrequency='1' 
          month='9' day='20' /></repeat>
       <windowEnd>2007-09-
          20T07:00:00Z</windowEnd>
    </rule></recurrence>
  5. 若要将基本会议工作区网站添加到定期事件,请使用 SPWebCollection 类的 Add() 方法创建网站(由 MPS#0 指定),并使用 SPMeeting 类的 LinkWithEvent 方法将网站与 Calendar 项目关联起来。

                Dim mwsSites As SPWebCollection = oWebsite.Webs
    
                Dim path As String = recEvent("Title").ToString()
    
                Dim newSite As SPWeb = mwsSites.Add(path, "Workspace_Name", "Description", 1033, _
                                                            "MPS#0", False, False)
    
                Dim mwsSite As SPMeeting = SPMeeting.GetMeetingInformation(newSite)
    
                Dim guid As String = list.ID.ToString()
                Dim id As Integer = recEvent.ID
    
                Try
                    mwsSite.LinkWithEvent(oWebsite, guid, id, "WorkspaceLink", "Workspace")
                Catch ex As System.Exception
    
                    Console.WriteLine(ex.Message)
    
                End Try
    
           End If
    
           oWebsite.Close()
    
        Next
    
    End Using
    
                SPWebCollection mwsSites = oWebsite.Webs;
    
                string path = recEvent["Title"].ToString();
    
                SPWeb newSite = mwsSites.Add(path, "MyNewWorkspace", "Description", 1033, "MPS#0", false, false);
    
                SPMeeting mwsSite = SPMeeting.GetMeetingInformation(newSite);
    
                string guid = list.ID.ToString();
                int id = recEvent.ID;
    
                try
                {
                    mwsSite.LinkWithEvent(oWebsite, guid, id, "WorkspaceLink", "Workspace");
                }
    
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
    
            oWebsite.Close();
        }
    }
    

    在创建会议工作区网站之后,GetMeetingInformation 方法将返回一个表示该网站的 SPMeeting 对象。

  6. 按 F5 可启动控制台应用程序。

  7. 在命令提示符下,键入会议工作区网站的名称,然后按 Enter 以将具有会议工作区网站的定期事件添加到网站集中所有网站的 Calendar 列表。