Wednesday, July 18, 2007

I've been working a bit with one of my colleagues, Slava, developing web parts and deploying them using Features. One issue that cropped up was how to remove the custom web parts from the Web Part gallery and any websites if the feature was subsequently deactivated. This kind of feature cleanup isn't something that can be done without custom code.

The solution Microsoft has provided is to create an event receiver called a Feature Receiver class for the feature. When tiggered, custom code can do any kind of setup, cleanup, or other job that might be required.

There are four events in the Feature life-cycle that can be triggered:
  1. FeatureInstalled
  2. FeatureUninstalling
  3. FeatureActivated
  4. FeatureDeactivating
At a high level the steps needed to create a Feature Receiver are:
  1. Create a strongly-named feature receiver assembly containing a class inheriting from SPFeatureReceiver
  2. Add custom code to the appropriate feature receiver methods
  3. Deploy the assembly to the GAC on the SharePoint server(s) where the feature is deployed
  4. Add a reference to the Feature Receiver assembly and class in the Feature.xml file
  5. Deploy the feature

The Microsoft SDK explains Features and feature receivers in detail. In the particular case that we ran into, Zac Smith has a great example explaining how to deprovision the web parts.

2 comments:

  1. Hi ,

    How can we Dynamically register an event receiver for a feature event...using object model.

    Thanks

    Regards
    Nimali

    ReplyDelete
  2. Hi Nimali, that's a good question. I've actually stayed away from using the SharePoint object model whenever possible for two reasons:
    1) The API wasn't well documented until recently
    2) I've tried to focus on what could be done "out of the box", mostly be configuration.
    So I can't answer this one off the top of my head. Let me try a little experiment and get back to you on the results.

    ReplyDelete

Note: only a member of this blog may post a comment.