Share via


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 Customizing Development Settings in Visual Studio.

Prerequisites

You need the following components to complete this walkthrough:

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

  1. On the menu bar, choose File, New, Project to display the New Project dialog box.

  2. Expand the SharePoint node under either Visual C# or Visual Basic, and then choose the 2010 node.

  3. In the Templates pane, choose the SharePoint 2010 Project template.

    You use this project type for feature event receivers because they have no project template.

  4. In the Name box, enter FeatureEvtTest, and then choose the OK button to display the SharePoint Customization Wizard.

  5. 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>/).

  6. In the What is the trust level for this SharePoint solution? section, choose the Deploy as a farm solution option button.

    For more information about sandboxed solutions versus farm solutions, see Sandboxed Solution Considerations.

  7. Choose the Finish button, and then notice that a feature that's named Feature1 appears under the Features node.

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

  1. Open the shortcut menu for the Features node, and then choose Add Feature to create a feature.

  2. Under the Features node, open the shortcut menu for Feature1, and then choose Add Event Receiver to add an event receiver to the feature.

    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.

  3. If your project is written in Visual C#, add the following code at the top of the event receiver if it is not already there:

    using System;
    
  4. The event receiver class contains several 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

  1. Set the value of 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.

  2. Choose the F5 key to run the project and deploy it to SharePoint.

  3. At the top of the SharePoint Web page, open the Site Actions menu, and then choose Site Settings.

  4. Under the Site Actions section of the Site Settings page, choose the Manage site features link.

  5. On the Features page, choose the Activate button next to the FeatureEvtTest Feature1 Feature.

  6. On the Features page, choose the Deactivate button next to the FeatureEvtTest Feature1 Feature, and then choose the Deactivate this feature confirmation link to deactivate the Feature.

  7. Choose the Home button.

    Notice that an announcement appears in the Announcements list after the feature is deactivated.

See Also

Tasks

How to: Create an Event Receiver

Other Resources

Developing SharePoint Solutions