SPMeeting.LinkWithEvent Method

Links a Meeting Workspace site with an item defined on a calendar events list in a Microsoft SharePoint Foundation Web site.

Namespace:  Microsoft.SharePoint.Meetings
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Function LinkWithEvent ( _
    eventWeb As SPWeb, _
    strEventListId As String, _
    eventItemId As Integer, _
    strEventWorkspaceLinkField As String, _
    strEventWorkspaceLinkURLField As String _
) As String
'Usage
Dim instance As SPMeeting
Dim eventWeb As SPWeb
Dim strEventListId As String
Dim eventItemId As Integer
Dim strEventWorkspaceLinkField As String
Dim strEventWorkspaceLinkURLField As String
Dim returnValue As String

returnValue = instance.LinkWithEvent(eventWeb, _
    strEventListId, eventItemId, strEventWorkspaceLinkField, _
    strEventWorkspaceLinkURLField)
public string LinkWithEvent(
    SPWeb eventWeb,
    string strEventListId,
    int eventItemId,
    string strEventWorkspaceLinkField,
    string strEventWorkspaceLinkURLField
)

Parameters

  • strEventListId
    Type: System.String

    A string that identifies the calendar component. You can get this value by performing a string conversion on the value of the SPList.ID property of the events list.

  • eventItemId
    Type: System.Int32

    A value that identifies the events list item. You can get this value from the SPListItem.ID property of the events list item.

  • strEventWorkspaceLinkField
    Type: System.String

    The internal name of the field on the events list item that receives the link. For an item that has the Event content type, this field is identified by the SPBuiltInFieldId.WorkspaceLink field identifier (ID) and has the internal name "WorkspaceLink".

  • strEventWorkspaceLinkURLField
    Type: System.String

    The internal name of the field on the events list item that displays the name of the workspace. For an item that has the Event content type, this field is identified by the SPBuiltInFieldId.Workspace field ID and has the internal name "Workspace".

Return Value

Type: System.String
The URL for the Meeting Workspace site.

Remarks

The LinkWithEvent method links an item in an events list, such as the default Calendar list, to a Meeting Workspace site. An events list is any list that has a BaseTemplate property value equal to SPListTemplateType.Events. A Meeting Workspace site is a Web site for which the IsMeetingWorkspaceWeb(SPWeb) method returns true.

The LinkWithEvent method creates a new meeting instance in the Meeting Workspace site. Then the method places the URL to the meeting instance in the event item field that is specified in the fourth parameter, strEventWorkspaceLinkField, and it places the name of the Meeting Workspace site in the field specified in the fifth parameter, strEventWorkspaceLinkURLField. (The parameter names are somewhat misleading. Just remember to pass the URL for the link, then the display text for the link.) When the list item is displayed on a form, the URL for the meeting instance is rendered as a link and the name of the workspace is rendered as link text. The link enables a user to navigate from the item in the events list to the workspace and from the workspace back to the list item.

Examples

The following console application shows how to add an item to the Calendar events list, create a workspace Web site, and then link the workspace to the event item by calling the LinkWithEvent method.

Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Meetings

Module ConsoleApp
   Sub Main()
      Using siteCollection As SPSite = New SPSite("https://localhost")
         Using webSite As SPWeb = siteCollection.OpenWeb()

            ' Get the calendar event list.
            Dim lists As SPListCollection = webSite.Lists
            Dim eventList As SPList = Nothing
            Dim list As SPList
            For Each list In lists
               If list.BaseTemplate = SPListTemplateType.Events Then
                  eventList = list
                  Exit For
               End If
            Next

            If eventList IsNot Nothing Then

               ' Add a new event to the calendar.
               Dim eventTitle As String = "Test"
               Dim eventLocation As String = "Your office"
               Dim eventStart As DateTime = DateTime.Now.AddDays(7)
               Dim eventDuration As Double = 0.5
               Dim calendarEvent As SPListItem = AddEventToCalendar(eventList, eventTitle, _
                                                 eventLocation, eventStart, eventDuration)


               ' Create a meeting workspace Web site.
               Dim mwsWeb As SPWeb = CreateMWSWeb(webSite, calendarEvent)
               If mwsWeb IsNot Nothing Then

                  ' Link the calendar event to the workspace.
                  Dim listId As String = eventList.ID.ToString("B")
                  Dim eventId As Integer = calendarEvent.ID

                  Dim meetingInfo As SPMeeting = SPMeeting.GetMeetingInformation(mwsWeb)
                  Dim mtgUrl As String = meetingInfo.LinkWithEvent(webSite, listId, eventId, _
                                                               "WorkspaceLink", "Workspace")

                  ' Print the workspace URL to the console.
                  Console.WriteLine(mtgUrl)

                  ' Clean up
                  mwsWeb.Dispose()

               End If

            End If

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub

   Function AddEventToCalendar(ByRef calendar As SPList, ByVal title As String, ByVal location As String, _
                               ByVal start As DateTime, ByVal duration As Double) As SPListItem
      Dim item As SPListItem = Nothing

      ' Test the list to be sure it is an events list .
      If Nothing Is calendar Or calendar.BaseTemplate <> SPListTemplateType.Events Then
         Return item
      End If

      ' Add the new item to the events list .
      item = calendar.Items.Add()
      item(SPBuiltInFieldId.UID) = Guid.NewGuid()
      item(SPBuiltInFieldId.Title) = title
      item(SPBuiltInFieldId.Location) = location
      item(SPBuiltInFieldId.StartDate) = start
      item(SPBuiltInFieldId.EndDate) = start.AddHours(duration)
      item(SPBuiltInFieldId.TimeZone) = calendar.ParentWeb.RegionalSettings.TimeZone.ID
      item.Update()
      Return item
   End Function

   Function CreateMWSWeb(ByRef webSite As SPWeb, ByVal calendarEvent As SPListItem) As SPWeb
      Dim path As String = calendarEvent.Title
      Dim workspaceTitle As String = calendarEvent.Title + " Workspace"
      Dim description As String = "Meeting workspace"
      Dim mwsWeb As SPWeb = Nothing

      Try
         mwsWeb = webSite.Webs.Add(path, workspaceTitle, _
                                   description, 1033, "MPS#0", _
                                   False, False)
      Catch ex As SPException 'A site of that name already exists.
         If SPMeeting.IsMeetingWorkspaceWeb(webSite.Webs(path)) Then
            mwsWeb = webSite.Webs(path)
         End If
      End Try

      Return mwsWeb
   End Function

End Module
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Meetings;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("https://localhost"))
         {
            using (SPWeb webSite = siteCollection.OpenWeb())
            {
               // Get the calendar event list.
               SPListCollection lists = webSite.Lists;
               SPList eventList = null;
               foreach (SPList list in lists)
               {
                  if (list.BaseTemplate == SPListTemplateType.Events)
                  {
                     eventList = list;
                     break;
                  }
               }
               if (null != eventList)
               {
                  // Add a new event to the calendar.
                  string eventTitle = "Test";
                  string eventLocation = "Your office";
                  DateTime eventStart = DateTime.Now.AddDays(7);
                  double eventDuration = 0.5;
                  SPListItem calendarEvent = AddEventToCalendar(eventList, eventTitle,
                                             eventLocation, eventStart, eventDuration);

                  // Create a meeting workspace Web site.
                  SPWeb mwsWeb = CreateMWSWeb(webSite, calendarEvent);
                  if (null != mwsWeb)
                  {
                     // Link the calendar event to the workspace.
                     string listId = eventList.ID.ToString("B");
                     int eventId = calendarEvent.ID;

                     SPMeeting meetingInfo = SPMeeting.GetMeetingInformation(mwsWeb);
                     string mtgUrl = meetingInfo.LinkWithEvent(webSite, listId, eventId,
                                                               "WorkspaceLink", "Workspace");

                     // Print the workspace URL to the console.
                     Console.WriteLine(mtgUrl);

                     // Clean up
                     mwsWeb.Dispose();
                  }
               }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
         }
      }

      static SPListItem AddEventToCalendar(SPList calendar, string title, string location,
                                           DateTime start, Double duration)
      {
         SPListItem item = null;

         // Test the list to be sure it is an events list .
         if (null == calendar || calendar.BaseTemplate != SPListTemplateType.Events)
            return item;

         // Add the new item to the events list .
         item = calendar.Items.Add();
         item[SPBuiltInFieldId.UID] = Guid.NewGuid();
         item[SPBuiltInFieldId.Title] = title;
         item[SPBuiltInFieldId.Location] = location;
         item[SPBuiltInFieldId.StartDate] = start;
         item[SPBuiltInFieldId.EndDate] = start.AddHours(duration);
         item[SPBuiltInFieldId.TimeZone] = calendar.ParentWeb.RegionalSettings.TimeZone.ID;
         item.Update();
         return item;
      }

      static SPWeb CreateMWSWeb(SPWeb webSite, SPListItem calendarEvent)
      {
         string path = calendarEvent.Title;
         string workspaceTitle = calendarEvent.Title + " Workspace";
         string description = "Meeting workspace";
         SPWeb mwsWeb = null;
         try
         {
            mwsWeb = webSite.Webs.Add(path, workspaceTitle,
                                      description, 1033, "MPS#0",
                                      false, false);
         }
         catch (SPException) // A site of that name already exists.
         {
            if (SPMeeting.IsMeetingWorkspaceWeb(webSite.Webs[path]))
               mwsWeb = webSite.Webs[path];
         }
         return mwsWeb;
      }
   }
}

See Also

Reference

SPMeeting Class

SPMeeting Members

Microsoft.SharePoint.Meetings Namespace

SPWebCollection.Add

Other Resources

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