Wednesday, April 24, 2013

SharePoint 2010 - Creating a Feature Receiver

In the previous post we discussed about Feature Receivers in SharePoint 2010. In this post we will create a sample feature receiver to understand its basic functionality.

Objective
1) We will create a new feature which will have an event receiver attached to it.
2) Whenever this feature is activated, a new list named "Sales" will be created.
3) Upon de-activating the feature the "Sales" list will automatically get deleted.

Steps for creating the Feature Receiver
1) Create an empty SharePoint 2010 project named "FeatureReceiverDemo".
2) Add a Feature named "FeatureReceiverDemo" (It can be any name).
3) Right click on the new feature and select "Add Event Receiver" as shown in the figure below.

4) A new class file name "FeatureReceiverDemo.EventReceiver.cs" will be created which has the code for overriding the feature receiver events in commented format.

5) As discussed in the previous post, a feature can have the following events which can be captured and functionalities can be attached to it:-
i) Feature Activation: Occurs when a feature has been activated
ii) Feature Deactivating: Occurs while a feature is deactivating
iii) Feature Installation: Occurs when a feature has been installed
iv) Feature Uninstalling: Occurs while a feature is un-installing
v) Feature Upgrading: Occurs while a feature is upgrading

6) To achieve our objective we will override the FeatureActivated event and add the code for creating a new list named "Sales" whenever this event is fired. Following is the code.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site != null)
            {
                SPWeb web = site.RootWeb;
                try
                {
                    SPList targetList = web.Lists["Sales"];
                }
                catch (ArgumentException)
                {
                    Guid listId = web.Lists.Add("Sales",
                    "Sales List created through code"SPListTemplateType.Events);
                    SPList list = web.Lists[listId];
                    list.OnQuickLaunch = true;
                    list.Update();
                }
            }
 
        }

7) Similarly we will override the FeatureDeactivating event and add the code for deleting the list named "Sales" whenever this event is fired. Following is the code.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site != null)
            {
                SPWeb web = site.RootWeb;
                try
                {
                    SPList list = web.Lists["Sales"];
                    list.Delete();
                }
                catch (ArgumentException)
                {
                }
            }
 
        }
8) Deploy the solution.

9) Go to Site Settings->Site Collection Features. Activate the feature "Feature Receiver Demo".


10) You will notice that a list named "Sales" has been created which is also available in Quick Launch.

11) Click on the "Sales" link on quick launch to view the list. Based on your preference  you can choose any template for your list using "SPListTemplateType" in the code.

12) Now go back to Site Settings->Site Collection Features. De-activate the feature "Feature Receiver Demo".

13) You will notice that the list named "Sales" has been deleted which is also removed from the Quick Launch.

I hope that this sample will provide a broader idea about the usage of event receivers.

References
1) Microsoft SharePoint 2010 Developer Reference, by Paolo Pialorsi - Book.

3 comments:

  1. Neal Can you add the screenshots over here.It is all blocked.

    ReplyDelete
  2. Such a helpful article..!!!!!!!!!!!!!!!!!!!!! Thanks....Neal

    ReplyDelete
  3. in visual studio Which Solution we can select means sandbx solution or farm solution?

    ReplyDelete