Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Microsoft SharePoint 2010 Development with Visual Studio 2010 Expert Cookbook

You're reading from  Microsoft SharePoint 2010 Development with Visual Studio 2010 Expert Cookbook

Product type Book
Published in Sep 2011
Publisher Packt
ISBN-13 9781849684583
Pages 296 pages
Edition 1st Edition
Languages
Author (1):
Balaji Kithiganahalli Balaji Kithiganahalli
Profile icon Balaji Kithiganahalli

Table of Contents (15) Chapters

Microsoft SharePoint 2010 Development with Visual Studio 2010: Expert Cookbook
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
1. Lists and Event Receivers 2. Workflows 3. Advanced Workflows 4. List Definitions and Content Types 5. Web Parts 6. Web Services and REST 7. Working with Client Object Model Index

Creating a Feature Receiver


Feature Receiver as the name indicates is an event handler written to handle events triggered by Features. In this recipe, we will handle some of the Feature events and add a message to a custom list in our site.

Getting ready

Create a custom list from the SharePoint user interface called FeatureEventReceiver. By default, SharePoint adds a column called Title. Add a new column called Message to this list. The end result should look like the following:

How to do it...

  1. Launch Visual Studio as an administrator.

  2. Create an empty SharePoint project and name it FeatureEventReceiver.

  3. Stick with the defaults and click on Finish on the New Solution Wizard. As this is an Empty SharePoint project, there are no items in the project.

  4. Add a new Feature to this project by right-clicking on the Features folder. This should add a new feature called Feature1 and should open the Feature Designer in the IDE.

  5. Right-click this new feature to add an Event Receiver. This should add a code file named Feature1.EventReceiver.cs. Uncomment FeatureActivated and FeatureDeactivating methods. The project structure should look similar to the following screenshot:

  6. Add the following method to the class Feature1EventReceiver:

    private void AddMessage(ref SPFeatureReceiverProperties properties, string sMessage)
            {
    
               
                 using(SPWeb web = properties.Feature.Parent as SPWeb)
                 {
                    SPList list = web.Lists["FeatureEventReceiver"];
                    SPListItem li = list.AddItem();
                    li["Title"] = properties.Feature.Definition.DisplayName;
                    li["Message"] = sMessage;
                    li.Update();
                    li = null;
                    list = null;
                }
            }
  7. Call this method from the FeatureActivated and FeatureDeactivating methods. Your code should look like the following:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                AddMessage(ref properties, "Feature Activated");
    
            }
    
            public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
                AddMessage(ref properties, "Feature Deactivating");
            }
  8. Put break points on both the FeatureActivated and FeatureDeactivating methods and build and run the solution. This will open the default browser with the site that was provided in the solution creation wizard. The debugger never stops at the break points that were set. Exit out of this site to close the debugger so that the Visual Studio can retract the solution as well.

  9. Navigate to your site and open up the FeatureEventReceiver list where you should see two list items corresponding to Feature Activated and Feature Deactivating message as shown here, even though you could not see your code getting executed:

How it works...

From the code perspective, it is very simple. All we are doing is from the Feature properties, we get the reference to the site where Feature is being activated and deactivated. From this site reference, we will get the list object and add new items to it.

When we built and ran the solution, behind the scenes, Visual Studio packaged the solution as .wsp file and executed all the commands for activating the solution. The debugger has not yet attached to the process and hence cannot stop at the break point provided.

Visual Studio first builds the solutions and during this process also deploys the solution and activates it. The next step in the process is to run the solution, during which time the Visual Studio debugger attaches itself to the w3wp.exe process. At this time, the Feature is already installed and activated and there is no way for the debugger to stop at the break point indicated. Closing the browser does not indicate that the Feature will be deactivated either and hence the break point for FeatureDeactivating never gets executed.

See also

  • Debugging a Feature Receiver recipe

You have been reading a chapter from
Microsoft SharePoint 2010 Development with Visual Studio 2010 Expert Cookbook
Published in: Sep 2011 Publisher: Packt ISBN-13: 9781849684583
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}