Walkthrough: Add Feature Event Receivers
Feature event receivers are methods that execute when one of the following feature-related events occurs in SharePoint:
A feature is installed.
A feature is activated.
A feature is deactivated.
A feature is removed.
This walkthrough demonstrates how to add an event receiver to a feature in a SharePoint project. It demonstrates the following tasks:
Creating an empty project with a feature event receiver.
Handling the FeatureDeactivating method.
Using the SharePoint project object model to add an announcement to the Announcements list.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.
Prerequisites
You need the following components to complete this walkthrough:
Supported editions of Microsoft Windows and SharePoint. For more information, see Requirements for Developing SharePoint Solutions.
Visual Studio 2010.
Creating a Feature Event Receiver Project
First, create a project to contain the feature event receiver.
To create a project with a feature event receiver
Display the New Project dialog box by pointing to New on the File menu, and then selecting New Project.
Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.
In the Templates pane, select Empty Project.
This project type is for feature event receivers because there is no project template for them.
In the Name box, type FeatureEvtTest and then click OK to display the SharePoint Customization Wizard.
On the Specify the site and security level for debugging page, enter the URL for the SharePoint server site to which you want to add the new custom field item, or use the default location (http://<system name>/).
In the What is the trust level for this SharePoint solution? section, click Deploy as a farm solution and then click Finish.
For more information about sandboxed solutions versus farm solutions, see Sandboxed Solution Considerations.
Adding an Event Receiver to the Feature
Next, add an event receiver to the feature and add code that executes when the feature is deactivated.
To add an event receiver to the feature
Create a feature by right-clicking the Features node and selecting Add Feature.
Notice that a feature named Feature1 appears under the Features node.
Add an event receiver to the feature by right-clicking Feature1 in the Features node and selecting Add Event Receiver.
This adds a code file under Feature1. In this case, it is named either Feature1.EventReceiver.cs or Feature1.EventReceiver.vb, depending on your project's development language.
The event receiver class contains four commented-out methods that act as events. Replace the FeatureDeactivating method with the following:
Public Overrides Sub FeatureDeactivating(ByVal properties As SPFeatureReceiverProperties) Try ' Get reference to SharePoint site. Dim site As SPSite = New SPSite("https://localhost") Dim web As SPWeb = site.OpenWeb("/") ' Get reference to Announcements list. Dim announcementsList As SPList = web.Lists("Announcements") ' Add new announcement to Announcements list. Dim oListItem As SPListItem = announcementsList.Items.Add oListItem("Title") = ("Deactivated Feature: " + properties.Definition.DisplayName) oListItem("Body") = (properties.Definition.DisplayName + (" was deactivated on: " + DateTime.Now.ToString)) oListItem.Update() Catch e As Exception Console.WriteLine(("Error: " + e.ToString)) End Try End Sub
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { try { // Get reference to SharePoint site. SPSite site = new SPSite("https://localhost"); SPWeb web = site.OpenWeb("/"); // Get reference to Announcements list. SPList announcementsList = web.Lists["Announcements"]; // Add new announcement to Announcements list. SPListItem oListItem = announcementsList.Items.Add(); oListItem["Title"] = "Deactivated Feature: " + properties.Definition.DisplayName; oListItem["Body"] = properties.Definition.DisplayName + " was deactivated on: " + DateTime.Now.ToString(); oListItem.Update(); } catch (Exception e) { Console.WriteLine("Error: " + e.ToString()); } }
Testing the Feature Event Receiver
Next, deactivate the feature to test whether the FeatureDeactivating method outputs an announcement to the SharePoint Announcements list.
To test the feature event receiver
Set the project's Active Deployment Configuration property to No Activation.
Setting this property prevents the feature from activating in SharePoint and lets you debug feature event receivers. For more information, see Debugging SharePoint Solutions.
Press F5 to run the project and deploy it to SharePoint.
At the top of the SharePoint Web page, click the Site Actions button and then click Site Settings.
Under the Site Actions section of the Site Settings page, click the Manage site features link.
In the Site Features page, click the Activate button next to the FeatureEvtTest Feature1 Feature.
In the Site Features page, click the Deactivate button next to the FeatureEvtTest Feature1 Feature. Click the confirmation link to deactivate the Feature.
Click the Announcements link on the Navigation bar.
Notice that an announcement appears in the Announcements list after the feature is deactivated.
See Also
Tasks
How to: Create an Event Receiver