Jaa


Working with the SPMeeting.LinkWithEvent() method

The scenario we’re focusing on is the following:

1. Create a new Team Site
2. Create a new Blank Meeting Workspace
3. Create two events on the Team Site’s Calendar
4. Run the code below in order to link these events to the workspace:

 using System;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Meetings;
using Microsoft.SharePoint.Deployment;

namespace LinkMeeting {
class Program {
  private static readonly string siteUrl = "https://srv2003-wss/tsite1";
  private static readonly string workspaceUrl = siteUrl + "/meeting";
  private static readonly string calendarUrl = siteUrl + "/Lists/Calendar";
  private static readonly string eventUrl_1 = calendarUrl + "/1_.000", eventUrl_2 = calendarUrl + "/2_.000";

  static void Main(string[] args) {
    SPWeb webSite = new SPSite(siteUrl).OpenWeb();
    LinkEvent(webSite, eventUrl_1, workspaceUrl);
    LinkEvent(webSite, eventUrl_2, workspaceUrl);
    webSite.Dispose();
  }

  private static void LinkEvent(SPWeb webSite, string eventUrl, string workspaceUrl) {
    SPWeb wsWeb = new SPSite(workspaceUrl).OpenWeb();
    SPMeeting meetingWs = SPMeeting.GetMeetingInformation(wsWeb);
    try {  
      SPListItem eventItem = webSite.GetListItem(eventUrl);
      Console.WriteLine("Linking event '{0}' ID {1} in calendar '{2}' to workspace '{3}'",
      eventItem.Name, eventItem.ID.ToString(),
      eventItem.ParentList.RootFolder.ServerRelativeUrl, wsWeb.ServerRelativeUrl);

      meetingWs.LinkWithEvent(eventItem.Web,
      eventItem.ParentList.ID.ToString(), // Calendar ID
      eventItem.ID, "WorkspaceLink", "Workspace");
      Console.WriteLine("Linked");
      wsWeb.Dispose();
    }
    catch (Exception ex) {
      Console.WriteLine("Link failed: {0}", ex.Message);
    } } } }

The output is the following, indicating that the first link operation succeeded, while the second failed:

 Linking event 'event1' ID 1 in calendar '/tsite1/Lists/Calendar' to workspace '/tsite1/meeting'
Linked
Linking event 'event2' ID 2 in calendar '/tsite1/Lists/Calendar' to workspace '/tsite1/meeting'
Link failed: Invalid file name 

And the cause? The SPWeb and SPSite objects for the calendar and workspace sites were not disposed after the initial LinkWithEvent() call.
So I've developed the following alternate code:

 using System;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Meetings;
using Microsoft.SharePoint.Deployment;

namespace LinkMeeting {
class Program {
  static string siteUrl = "https://srv2003-wss/tsite2";
  static string wsUrl = "https://srv2003-wss/tsite2/meeting";

  static void Main(string[] args) {
    for (int i = 0; i <= 1; i++) {
      SPSite ws = null; SPWeb ww = null;
      SPSite ms = null; SPWeb mw = null;
      try {
        ms = new SPSite(siteUrl); mw = ms.OpenWeb();
        ws = new SPSite(wsUrl); ww = ws.OpenWeb();
        SPListItem li = mw.Lists["Calendar"].Items[i];
        Console.WriteLine("linking " + li.Name);
        SPMeeting mm = SPMeeting.GetMeetingInformation(ww);
        mm.LinkWithEvent(li.Web, li.ParentList.ID.ToString(), li.ID, "WorkspaceLink", "Workspace");
      }
      catch (Exception ex) {
        Console.WriteLine(ex.Message);
        Console.WriteLine(ex.StackTrace);
      }
      finally {
        if (ww != null) ww.Dispose();
        if (ws != null) ws.Dispose();
        if (mw != null) mw.Dispose();
        if (ms != null) ms.Dispose();
      } } } } }

It's interesting how re-building the SPSite and SPWeb objects "fixes" certain scenarios like the one above.