Thursday, June 19, 2008

Programmatically Create Workspace Linked to an Event (LinkWithEvent)

In my SharePoint 2007 project, I created a workspace template that I wanted the customers to always use when they created their meeting workspace. So I needed to bypass the standard site creation page and programmatically add the sub site with the desired template.

Step 1 was to create my own custom list template based on the Calendar list. I need to do this so I could hide the Workspace column from the user. But more importantly, so I could add an event receiver that would do the work of creating the workspace site.

Step 2 was to create the event receiver for the ItemAdded event via a feature.

The feature.xml file is standard. Being sure to set the scope to "Web" as receivers cannot be scoped at the Site Collection level.

 

 


<?xml version="1.0" encoding="utf-8"?>


<Feature  Id="YOURGUIDHERE"


          Title="CalendarEventListEventReceiver"


          Description="Description for CalendarEventListEventReceiver"


          Version="12.0.0.0"


          Hidden="FALSE"


          Scope="Web"


          DefaultResourceFile="core"


          xmlns="http://schemas.microsoft.com/sharepoint/">


  <ElementManifests>


    <ElementManifest Location="elements.xml"/>


  </ElementManifests>


</Feature>









 






For the elements.xml ensure that the ListTemplateId attribute references the type of your list template.














 


<?xml version="1.0" encoding="utf-8" ?>


<Elements xmlns="http://schemas.microsoft.com/sharepoint/">


  <Receivers ListTemplateId="10002">


    <Receiver>


      <Name>ItemAdded</Name>


      <Type>ItemAdded</Type>


      <SequenceNumber>10000</SequenceNumber>


      <Assembly>Events.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=747f099a9bd1475c</Assembly>


      <Class>Events.Web.EventListEventReceiver</Class>


      <Data></Data>


      <Filter></Filter>


    </Receiver>


  </Receivers>


</Elements>










Now create the receiver and override the ItemAdded method. Remember that the ItemAdded event is an asynchronous event, so you might not see your new site right away.






 




 


using System;


using System.Collections.Generic;


using System.Text;


using Microsoft.SharePoint;


using Microsoft.SharePoint.Meetings;


 


namespace MyNamespace


{


    public class EventListEventReceiver : SPItemEventReceiver


    {


        public override void ItemAdded(SPItemEventProperties properties)


        {


            CreateWorkSpace(properties);


            base.ItemAdded(properties);


        }


 


        private void CreateWorkSpace(SPItemEventProperties properties)


        {


            using (SPWeb web = properties.OpenWeb())


            {


                try


                {


                    //find the parent site


                    using (SPSite site = web.Site)


      {


                    // get a list of the custom templates so we can find our event template


                SPWebTemplateCollection templates = site.GetCustomWebTemplates(1033);


                       SPWebTemplate template = templates["EventWorkspaceTemplate.stp"];


                       // create the new web site


                       SPWeb newWeb = web.Webs.Add(properties.ListItemId.ToString(), properties.ListItem["Title"].ToString(), properties.ListItem["Location"].ToString(), 1033, template, false, false);


                       // cast the web as a meeting so we can link it back to the event item


                       SPMeeting meeting = SPMeeting.GetMeetingInformation(newWeb);


                       meeting.LinkWithEvent(web, properties.ListId.ToString(), properties.ListItemId, "WorkspaceLink", "Workspace");


      }


                }


                catch (Exception ex)


                {


                    properties.Cancel = true;


                    properties.ErrorMessage = ex.Message;


                }


            }


        }


    }


}








After installing the feature, be sure it is activated in the web site that your custom list exists.

5 comments:

Johannes said...

Don't forget to dispose of your site object ;)

Rusty said...

Thank you Johannes. Updated.

Sruli Ganor said...

Thanks, I used this sample and it worked nicely. I now have another problem: I need to disconnect a recurring event from its meeting workspace. I tried SPMeeting.Cancel but the MSDN info is totally useless and there are no samples anywhere. Could you provide a sample?

Thanks in advance
Sruli

Rusty said...

Sorry Sruli, I could not figure out how to do that either.

Sruli Ganor said...

Thanks anyway, Rusty.
Looks I'm in trouble... I found this info:
http://social.msdn.microsoft.com/forums/en-US/sharepointdevelopment/thread/e4cad3e4-d5f8-49e0-81df-85ce4ab248cc

but it did not work either :-(

Sruli