Feature Receivers

A Feature Receiver, simply put, is code that you (the developer) can choose to "attach" to a specific feature and that code will (generally) run when either you activate or deactivate the feature. More specifically, when you click on the Activate or Deactivate button.

Feature Receivers are important as they provide you the opportunity to run any feature initialization that is required on Activate, and any clean up that is required on Deactivate. In our case, we wish to do the following:

  • On Activate: Set the Management of Content Types setting for the the Proposals list so we can "see" that the correct Proposal Content Type is configured with the list. Code is the only way to accomplish changing this setting.
  • On Deactivate: Nothing happens to created lists, such as Proposals, when a Feature is deactivated. We are going to make the decision, for the sake of our example, that when the feature is deactivated, then the Proposals list will be deleted.

How to do it...

Open the SP2010ProposalLibrary Visual Studio Solution you created in the previous recipe:

  1. Underneath the Features node of the project, right-click on the ProposalLibrary Feature and select Add Event Receiver.
  2. In the ProposalLibrary.EventReceiver.cs file that opens, uncomment the methods for FeatureActivated and FeatureDeactivating, and delete the remaining lines of comments.

    Provide the following lines of code for the FeatureActivated method:

    SPWeb site = properties.Feature.Parent as SPWeb;
    // Enable Management of Content Types for Proposals Library
    SPList listProposalLibrary = site.Lists["Proposals"];
    listProposalLibrary.ContentTypesEnabled = true;
    listProposalLibrary.Update();
    
  3. Provide the following lines of code for the FeatureDeactivating method:
    SPWeb site = properties.Feature.Parent as SPWeb;
    if (site != null)
    {
    SPList list = site.Lists.TryGetList("Proposals");
    if (list != null)
    {
    list.Delete();
    }
    }
    

Done! That was easy!

Now, let us deploy the Solution and observe the results:

  1. Right-click the SP2010ProposalLibrary project and select Deploy Solution. Wait until you see the Deploy Succeeded message in the bottom left-hand corner of Visual Studio.
  2. Select Site Actions | Site Settings. Click on Manage Site Features that appears under the Site Actions section. Note that the Proposal Library is there and is activated:
    How to do it...
  3. Click on the Proposals link in the quick launch (left-hand menu). From the Ribbon Library tab, select Library settings. If you see the Content Types section exposed with our Proposal Content Type.

How it works...

  1. This is an evidence that our Feature Activated code has executed. This should be clear, as we were working with the FeatureActivated method just a few minutes ago in the How to do it... section.
    How it works...
  2. Select Site Actions | Site Settings. Click on Site Collection Features that appears under the Site Collection Administration section. For the Proposal Library Feature, click on the Deactivate button. You will get a warning You are about to deactivate the Proposal Library feature, click on the Deactivate this feature link. Note that the Active text disappears and the button has changed to Activate:
    How it works...
  3. Now we have evidence that our Feature Deactivating code has executed, as you will see that the Proposals document library no longer appears in the quick launch it has been deleted.

Actually, this is the first C# code you have written in this chapter! You have seen so far in this chapter that much can be accomplished through declarative (using XML) provisioning instructions.

Anything is possible as far as initialization and clean up goes with Feature Receivers. For example, you may have decided to pre-populate your list with sample data. You will find many examples in your future where you will want to take advantage of the powerful capabilities provided by Feature Receivers!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.59.163