Debugging a FeatureInstalled event handler in SharePoint 2010

Updated: An even easier method to do this is to simply launch the debugger by adding a statement to the beginning of the method for Systems.Diagnostics.Debugger.Launch.  Thanks to the suggestion I received on the topic - I misplaced the email, so the input will need to remain anonymous...

Updated: Debugging Feature Activated - so I said below that debugging feature activated is easy, which is true especially for site and web scoped features, but may not be obvious if you haven't done it before.  To debug FeatureActivated,

1) Properties->SharePoint, change Active Deployment Configuration to No Activation

2) Start the project in the debugger.

3) Activate the feature through site settings (assuming it’s a site or web scoped feature).

 

Original Post


 

Recently I had some code that I had to put in feature installed.  Debugging FeatureActivated is no problem in VS2010, but FeatureInstalled isn't so simple.  The feature installed event gets fired when a feature is installed to the farm.  It runs under a higher security context than feature activate, therefore you can do things like write settings to the farm or web application.  In my case my feature installed code had a bug, but I wasn't sure how I could hook it.  I found one approach at least that did work, so I figured I'd blog about it.

There are three possibilities for debugging the FeatureInstalled (this is for a Farm Solution):

1. The FeatureInstalled is throwing an exception and deploy fails in VS.  It's OK that the FeatureInstalled ran partially, and you can rerun to debug.

2. The FeatureInstalled is not throwing an exception, but its OK that it ran once and you can run it again.

3. You can't have the FeatureInstalled event run partially or completely prior to debugging.

The first 2 cases are actually pretty simple.  First deploy the project from VS.  This will put everything in the right place, even if an exception is thrown in your FeatureInstalled, and it looks like the deploy failed.  Then follow the following process.

One way that features are commonly installed is from the command line using STSADM or powershell.  You can use this to hook into the debugging process.  In this case I will use the "TestFarmSettings" project as an example.  I did the following in order to debug the feature installed:

 

  1.  Added a command line project to my solution.
    1. set build, platform target to any cpu (under build tab), and set project to the 3.5 framework (under application tab)
    2. On the debug tab, set start external program, and set the external program to "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\stsadm.EXE"
    3. Add a reference to my sharepoint project that has the feature installed you want to debug.
  2. Set the breakpoint on myFeatureInstalled event handler.
  3. Deploy the SharePoint project from VS.  It is OK if the deploy fails due to an exception thrown in the feature receiver.
  4. Add the following for the command line options for the console application in the debug tab: -o installfeature -filename <your feature name>\feature.xml -force.  For example, I used -o installfeature -filename TestFarmSettingStore_Feature1\feature.xml -force. 
  5. Set the console application as the startup project
  6. Hit F5.  Your breakpoint will be hit.

Case 3 its more complicated since you need to get the files into the right places yourself.  As a little background, the reason why you run the deploy is to get the solution added and installed, and get the debug information deployed for the gac'd assembly.  If you do the add and install solution methods manually, then run the debug files don't get into the right place for the assembly. 

You will need to run some manual steps to setup the conditions:

  1. You will need to add the solution, which should be in the debug\bin output of your project.  stsadm.exe is located in "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\".
  2. Add the solution, for example stsadm -o addsolution -filename TestFarmSettingStore.wsp
  3. Deploy the solution, for example, >stsadm -o deploysolution -name testfarmsettingstore.wsp -url https://localhost -immediate -allowGacDeployment

Now you are ready to actually install a feature. Remember that by default farm and web app scoped features will install automatically when the deploy solution step is ran, and so you will need to change this behavior for those solution types.  This is controlled by the ActivateOnDefault property for the feature (see Feature Element (Feature))

You will need to copy the debug file (.pdb) into the location that your gac'd assembly gets installed.  You can find that location by running dir /s c:\windows\assembly\<your assembly name>.dll.   Once the debug file is located beside the assembly, you can then perform the steps outlined with the console app.  You will be able to break on the FeatureInstalled event the first time you run it.

 If anyone has an easier way, please let me know!