Compartilhar via


How to: Add a Recurring Event to Lists on Multiple Sites

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

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

To add a recurring event with a Meeting Workspace site to the Calendar list of every site in a site collection

  1. Create a console application in Microsoft Visual Studio 2010, as described in How to: Create a Console Application.

  2. 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.

    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.Meetings
    
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Meetings;
    
  3. Use the SPSite constructor to instantiate a specified site collection. This example uses the AllWebs property of the SPSite class to return all the Web sites within the site collection. The example uses a foreach loop to enumerate the Web sites and, if a Web site was created through the standard SharePoint Foundation template (WebTemplateSTS), the example obtains the Calendar list for the site and the collection of list items in the list.

    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. To create a list item, use the Add method of the SPListItemCollection class to create an uninitialized list item, use indexers to set various properties for the new item, and then use the Update method to finish creating the item.

    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();
    

    The recData variable contains an XML fragment that specifies properties for a recurring event taking place daily for five days, and the XMLTZone indexer assigns time zone information for the current site. The XML that defines the recurrence and 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>
  5. To add a Basic Meeting Workspace site to the recurring event, use the Add() method of the SPWebCollection class to create the site (specified by MPS#0), and the LinkWithEvent method of the SPMeeting class to link the site with the Calendar item.

                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();
        }
    }
    

    After the Meeting Workspace site is created, the GetMeetingInformation method returns an SPMeeting object that represents the site.

  6. Press F5 to start the Console Application.

  7. 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 Calendar list of all the Web sites within a site collection.