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
- a feature that creates an Employee list and a Project list
- 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
- public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
- {
- using (SPWeb spWeb = (SPWeb)properties.Feature.Parent)
- {
- SPList empList = spWeb.Lists["Employees"];
- empList.Delete();
- spWeb.Update();
- SPList projList = spWeb.Lists["Projects"];
- projList.Delete();
- spWeb.Update();
- }
- }
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
- public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
- {
- using (SPSite spSite = (SPSite)properties.Feature.Parent)
- {
- using (SPWeb spWeb = (SPWeb)spSite.OpenWeb())
- {
- SPList empList = spWeb.Lists["Employees"];
- empList.Delete();
- spWeb.Update();
- SPList projList = spWeb.Lists["Projects"];
- projList.Delete();
- spWeb.Update();
- }
- }
- }
I made same change in the “FeatureActivated” method of the event listener. Now, I can deploy and retract in a single package.