Share via


Exercise 2: Adding a Feature Event Receiver

In exercise 1 you learned how to modify project properties and features properties using visual designers and property sheets. Now you are going to add some code. Over the next few steps, you are going to add a feature receiver so you can write code against the SharePoint object model that will automatically execute during feature activation and feature deactivation.

  1. Add an event receiver by right-clicking on the Main feature node in the Solution Explorer and clicking on the context menu with the caption of Add Event Receiver.

    Figure 1

    Add an event receiver. C# shown, VB.NET similar for Add Event Receiver.

  2. Open the source file named Main.EventReceiver.cs/Main.EventReceiver.vb and inspect what is inside. You should be able to see that there are several method stubs inside the class definition that are commented out. There is also a GUID attribute that has been applied to the receiver class to give a unique identifier. Do not remove the GUID from the class because it will be used behind the scenes during the packaging process. Uncomment the two methods named FeatureActivated and FeatureDeactivating. Then remove all the extraneous comments so your code looks like this.

    C#

    using System; using System.Runtime.InteropServices; using System.Security.Permissions; using Microsoft.SharePoint; using Microsoft.SharePoint.Security; namespace ContosoWebParts.Features.Main { // your GUID will be different [Guid("19d897a9-1520-48aa-87e1-0eaa12c08c88")] public class MainEventReceiver : SPFeatureReceiver { public override void FeatureActivated( SPFeatureReceiverProperties properties) { } public override void FeatureDeactivating( SPFeatureReceiverProperties properties) { } } }

    VB.NET

    Option Explicit On Option Strict On Imports System Imports System.Runtime.InteropServices Imports System.Security.Permissions Imports Microsoft.SharePoint Imports Microsoft.SharePoint.Security ' your GUID will be different <GuidAttribute("8bea6fc3-3573-4927-93a3-0ca62cb3985e")> _ Public Class MainEventReceiver Inherits SPFeatureReceiver Public Overrides Sub FeatureActivated(ByVal properties As _ SPFeatureReceiverProperties) End Sub Public Overrides Sub FeatureDeactivating(ByVal properties As _ SPFeatureReceiverProperties) End Sub End Class
  3. Implement the FeatureActivated method to modify the Title property and the ImageUrl property of the top-level site and to call the Update method to save this modification back to the content database. Before changing the Title property, your code should track its original value so that your feature can restore it upon feature deactivation. Implement the FeatureActivated method using the following steps.
    1. Obtain the SPSite reference which point to the site collection in which the feature is being activated.
    2. Obtain a SPWeb reference which points to the top-level site
    3. Query to top-level site's Title property and store the value as a name/value pair inside the SPWeb.Properties collection.
    4. Modify the site's Title property with a creative text value such as "VS2010 SPT Rocks"
    5. Modify the site's SiteLogoUrl property to point to SiteIcon.gif inside the Images folder.

      _layouts/images/ContosoWebParts/SiteIcon.gif;
    6. Your code should like something like this.

      C#

      public override void FeatureActivated(
      FakePre-7d718927c1774725be40c921b52a3b39-09472b29cdf043518cdf5d438e9c4c23FakePre-afda726174d24439b0ec24cb7fca9590-4f8fada231794edab13a0ea9b974e7d2FakePre-a63133ef902549d792972f512d49c843-8a0cc2fdf6434699b3fb80498f6f7cb6FakePre-03913795ccf54fc8a053399700f26899-c1a07e9597784e428aa4f3b91cae6568FakePre-1da9a0dcf46445759beec455acf65084-1401f77cf521496f87a01b8665057378FakePre-6636dbfad75e4e9387e11bcb63c86f9e-a14032fcba5946f08f06752434e939afFakePre-a1f5bd33af244180a5cd8d33dabc3be4-5ed45ca0a08644338a0e933f98fcc25bFakePre-f0976e4cbb3345878e538e2954ad972e-292d593b9d944e1abea62a942b7a4d06FakePre-f9929e84ed1447bdaf0d6e15003ba092-6a727b92674c40458bf9b2ddde114738FakePre-32ca2934e4b041d2a2d2691bdde2e076-c4c5608e10cc45eda3bf5d9ec5bf6798FakePre-df95abce5e464fb3a3301a31b6d1cf10-5cde3954c50b4208aa7f5b82edff0d76FakePre-c42bf486381f495f929f2d4d06bd49eb-e72f45f5f7d44c8d8452adda5f7c1158FakePre-1a9e8a630e8c4f4c80b8aab7e76caf3b-b0b89ac6da624e5eb7881c6cc878bd50FakePre-92b49a0fa3f1486da1a5609a9f718191-a6a123d4fb8847af8d16ffaddad28b1cFakePre-ca7e0b9ff5404bc981b8f44feba49e46-ae76671d6e934b4b9a32df1a961a5273 }FakePre-0db8d10a5c0e4b19b47c02e0f87fbec7-83f65d108a6d450c88b1432b97f66e36

      VB.NET

      Public Overrides Sub FeatureActivated(properties As SPFeatureReceiverProperties) Dim siteCollection As SPSite = TryCast(properties.Feature.Parent, SPSite) If siteCollection IsNot Nothing Then ' save top site's original Title and SiteLogoUrl Dim site As SPWeb = siteCollection.RootWeb site.Properties("OriginalTitle") = site.Title site.Properties.Update() ' update the Title and SiteIconUrl site.Title = "VS 2010 SPT Rocks" site.SiteLogoUrl = "_layouts/images/ContosoWebParts/SiteIcon.gif" site.Update() End If End Sub
  4. Now, implement FeatureDeactivating so that your code restores the original Title property value and changes the SiteLogoUrl property value back to an empty string.

    C#

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPSite siteCollection = properties.Feature.Parent as SPSite; if (siteCollection != null) { // restore top site's original Title and SiteLogoUrl SPWeb site = siteCollection.RootWeb; site.Title = site.Properties["OriginalTitle"]; site.SiteLogoUrl = string.Empty; site.Update(); } }

    VB.NET

    Public Overrides Sub FeatureDeactivating(properties As SPFeatureReceiverProperties) Dim siteCollection As SPSite = _ TryCast(properties.Feature.Parent, SPSite) If siteCollection IsNot Nothing Then ' restore top site's original Title and SiteLogoUrl Dim site As SPWeb = siteCollection.RootWeb site.Title = site.Properties("OriginalTitle") site.SiteLogoUrl = String.Empty site.Update() End If End Sub
  5. Right-click on the ContosoWebParts project in Solution Explorer and run the Build command. This will ensure that all the code you have written in this exercise compiles. If you have compilation errors in your code, fix them until you are able to run the Build command without any errors.
Note:
You have now implemented the feature receiver. Save all your work and move on to the next exercise where you will deploy and test this code.