Book Image

Microsoft SharePoint 2010 Development with Visual Studio 2010 Expert Cookbook

By : Balaji Kithiganahalli
Book Image

Microsoft SharePoint 2010 Development with Visual Studio 2010 Expert Cookbook

By: Balaji Kithiganahalli

Overview of this book

Microsoft SharePoint 2010, is the best-in-class platform for content management and collaboration. With the combined capabilities of Sharepoint and Visual Studio, developers have an end-to-end business solutions development IDE. To leverage this powerful combination of tools it is necessary to understand the different building blocks. This book will provide necessary concepts and present ways to develop complex business solutions and take them further.SharePoint 2010 Development Cookbook With Visual Studio 2010 is an instructional guide for developing and debugging applications for SharePoint 2010 environment using Visual Studio 2010. The cookbook approach helps you to dip into any recipe that interests you, you can also read it from cover to cover if you want to get hands on with the complete application development cycle.With this book you will learn to develop event handlers, workflows, content types, web parts, client object model applications, and web services for SharePoint 2010 in an instructional manner. You will discover the less known facts behind debugging feature receivers, deployment of web parts, utilizing free toolkits to enhance the development and debugging experience. You will learn the newer development approach called Visual Web Parts, how to develop and deploy Silverlight applications that can be used with Silverlight web part. You will also explore SandBoxed deployment model and its usage. You will create your own web services for SharePoint and the Client Object Model introduced in SharePoint 2010. All in all, you will develop Sharepoint solutions in an instructional manner that eases the learning process.
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
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