Export (0) Print
Expand All

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.

NoteNote

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.

You need the following components to complete this walkthrough:

First, create a project to contain the feature event receiver.

To create a project with a feature event receiver

  1. Display the New Project dialog box by pointing to New on the File menu, and then selecting New Project.

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

  3. In the Templates pane, select Empty Project.

    This project type is for feature event receivers because there is no project template for them.

  4. In the Name box, type FeatureEvtTest and then click OK 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, click Deploy as a farm solution and then click Finish.

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

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. Create a feature by right-clicking the Features node and selecting Add Feature.

    Notice that a feature named Feature1 appears under the Features node.

  2. 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.

  3. The event receiver class contains four commented-out methods that act as events. Replace the FeatureDeactivating method with the following:

    
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        try
        {
            // Get reference to SharePoint site.
            SPSite site = new SPSite("http://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());
        }
    
    }
    
    
    

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 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. Press F5 to run the project and deploy it to SharePoint.

  3. At the top of the SharePoint Web page, click the Site Actions button and then click Site Settings.

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

  5. In the Site Features page, click the Activate button next to the FeatureEvtTest Feature1 Feature.

  6. In the Site Features page, click the Deactivate button next to the FeatureEvtTest Feature1 Feature. Click the confirmation link to deactivate the Feature.

  7. Click the Announcements link on the Navigation bar.

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

Community Additions

ADD
Show:
© 2014 Microsoft