Compartilhar via


Adding a Recurring Event to Lists on Multiple Sites

This programming task shows how to add a recurring event with a Meeting Workspace site to the Events list of every site in a collection of subsites.

  • Create a console application in Microsoft Visual Studio .NET, as described in Creating a Console Application.

  • Add a using or Imports directive to the opening of the .cs or .vb file for the Microsoft.SharePoint and Microsoft.SharePoint.Meetings namespaces, as follows:

    [Visual Basic .NET]
    
    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.Meetings
    
    [C#]
    
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Meetings;
    
  • Use the SPSite constructor to instantiate a specified site collection. This example uses an indexer on the AllWebs property of the SPSite class to return a specific site, and the Webs property of the SPWeb class to return the collection of subsites beneath the site. Set up a foreach loop to iterate through all the subsites and obtain the Events list for each site and the collection of list items in each Events list, as follows:

    [Visual Basic .NET]
    
    Dim evtTitle As String = Console.ReadLine()
    
    Dim siteCollection As New SPSite("Absolute_Url")
    Dim site As SPWeb = siteCollection.AllWebs("Site_Name")
    Dim subSites As SPWebCollection = site.Webs
    Dim subSite As SPWeb
    
    For Each subSite In subSites
    
        Dim list As SPList = subSite.Lists("Events")
        Dim listItems As SPListItemCollection = list.Items
    
    [C#]
    
    string evtTitle = Console.ReadLine();
    
    SPSite siteCollection = new SPSite("Absolute_Url");
    SPWeb site = siteCollection.AllWebs["Site_Name"];
    SPWebCollection subSites = site.Webs;
    
    foreach (SPWeb subSite in subSites)
    {
        SPList list = subSite.Lists["Events"];
        SPListItemCollection listItems = list.Items;
    
  • To create a list item, the example uses the Add method of the SPListItemCollection class to create an unitialized list item, uses indexers to set various properties for the new item, and then uses the Update method to finish creating the item.

    [Visual Basic .NET]
    
    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(2003, 8, 15, 8, 0, 0)
    recEvent("EndDate") = New DateTime(2003, 9, 25, 9, 0, 0)
    recEvent("UID") = 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()
    
    [C#]
    
        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(2003,8,15,8,0,0);
        recEvent["EndDate"] = new DateTime(2003,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();
    

    The recData variable contains an XML fragment that specifies properties for a recurring event taking place daily for five days, and the XMLTZone indexer is used to assign time zone information for the current site. The XML that defines the recurrence and that specifies the time zone information is contained in the ntext3 and ntext4 columns of the UserData table in the content database.

    The following table shows examples of the different kinds of recurrence that can be used.

    Description Example

    Every other day until a specified end date

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

    Weekly on Monday

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

    Every two months on the third day for five sessions

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

    Monthly on the first Tuesday until a specified end date

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

    Yearly on the twentieth day of the ninth month until a specified end date

    <recurrence><rule>
       <firstDayOfWeek>su</firstDayOfWeek>
       <repeat><yearly yearFrequency='1' month='9' day='20' /></repeat>
       <windowEnd>2007-09-20T07:00:00Z</windowEnd>
    </rule></recurrence>
  • To add a Meeting Workspace site to the recurring event, use one of the Add methods of the SPWebCollection class and the LinkWithEvent method of the SPMeeting class.

    [Visual Basic .NET]
    
        Dim mwsSites As SPWebCollection = subSite.Webs
    
        Dim path As String = recEvent("Title").ToString()
    
        Dim newSite As SPWeb = mwsSites.Add(path, "Workspace_Name", _
            "Description", Convert.ToUInt32(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(subSite, guid, id, "WorkspaceLink", "Workspace")
    
        Catch ex As System.Exception
    
            Console.WriteLine(ex.Message)
    
        End Try
    
    Next subSite
    
    [C#]
    
        SPWebCollection mwsSites = subSite.Webs;
    
        string path = recEvent["Title"].ToString();
    
        SPWeb newSite = mwsSites.Add(path, "Workspace_Name", "Description", 1033, "MPS#0", false, false);
    
        SPMeeting mwsSite = SPMeeting.GetMeetingInformation(newSite);
    
        string guid = list.ID.ToString();
        int id = recEvent.ID;
    
        try
        {
            mwsSite.LinkWithEvent(subSite, guid, id, "WorkspaceLink", "Workspace");
        }
    
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    

    After the Meeting Workspace site is created, the GetMeetingInformation method is used to return an SPMeeting object representing the site.

  • Press F5 to start the Console Application, at the command prompt type a name for the Meeting Workspace site, and then press ENTER to add a recurring event with a Meeting Workspace site to the Events list in all the subsites beneath a site.