SharePoint 2010 Visual Studio Training Kit

There is a SharePoint 2010 Developer Training Course over at the Channel 9 Learning Center.  There are a few places where I tweaked things a bit to make it work the way I wanted. 

One of the labs is a LINQ to SharePoint lab. It has two parts

  1. a feature that creates an Employee list and a Project list
  2. a web part that queries the Employee list using LINQ.

By default, the list creation feature has “Web" scope, and the sample code relies on that scope—it casts “properties.Feature.Parent” to an SPWeb object in the FeatureActivated and FeatureDeactivating methods.  The Web Part that depends on that feature has “Site” scope, and has to have “Site” scope. 

Here’s the original method—note line #3:

Original FeatureDeactivating

  1. public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  2.       {
  3.           using (SPWeb spWeb = (SPWeb)properties.Feature.Parent)
  4.           {
  5.               SPList empList = spWeb.Lists["Employees"];
  6.               empList.Delete();
  7.               spWeb.Update();
  8.               SPList projList = spWeb.Lists["Projects"];
  9.               projList.Delete();
  10.               spWeb.Update();
  11.           }
  12.       }

Since I wanted to make the dependence explicit, I had to elevate the scope of the first feature to “Site” scope.  Of course, that breaks the sample code. I revised the code to acquire the SPSite object and retrieved the web site from that object.  Line 3 was replace by lines 3 through 5.

Revised FeatureDeactivating

  1. public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  2.         {
  3.             using (SPSite spSite = (SPSite)properties.Feature.Parent)
  4.             {
  5.                 using (SPWeb spWeb = (SPWeb)spSite.OpenWeb())
  6.                 {
  7.                     SPList empList = spWeb.Lists["Employees"];
  8.                     empList.Delete();
  9.                     spWeb.Update();
  10.                     SPList projList = spWeb.Lists["Projects"];
  11.                     projList.Delete();
  12.                     spWeb.Update();
  13.                 }
  14.             }
  15.         }

I made same change in the “FeatureActivated” method of the event listener.  Now, I can deploy and retract in a single package.