List Events Handling in SharePoint 2007

No.of Views1144
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : SharePoint , Development and Programming
List Events Handling in SharePoint 2007
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

The objective of this article is to give a very basic explanation of how to catch an event on a SharePoint list and deploy them programmatically. I will achieve in this article “User will get error message; whenever any item will get deleted from a particular list “.

What is Event?

Event handling gives developer the ability to catch certain user actions in code and react programmatically.

Steps to handle List in SharePoint

1. Create a class that will extend SPListEventReceiver or SPItemEventReceiver class. These are classes of Microsoft.SharePoint.dll assembly.

2. Sign the class library with strong name.
3. Deploy the class library to GAC
4. Deploy the event receiver assembly. 

Step 1: Creating the class

Create a new project of type class library. I am giving name of the project eventhandlersampleclasslibrary. You are free to give any name of your choice. I am extending the SPItemEventReceiver. The purpose of extending this class is that I am going to handle events on a particular item of a list.

Note: I am going to handle the Item Deleting event on a particular list. I will not allow any user to delete any item in that list.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace eventhandlersampleclasslibrary
{public class Class1 : SPItemEventReceiver
    {public override void ItemDeleting(SPItemEventProperties properties)
        {//base.ItemDeleting(properties);string errorMessage = "Please Do not delete ; you are not supposed too ";
            properties.ErrorMessage = errorMessage;
            properties.Cancel = true;
        }
    }
}

Step 2: Signing the class name

1. Right click on Project and then Properties.
2. Now click on signing tab.
3. Check the Sign the Assembly checkbox.
4. For selecting the Strong name key file , select New option from drop down
5. Give a strong name. I am giving the name abc here.
6. Make sure Password protection checkbox is unchecked.
7. After this; you would be able to see abc.snk in solution explorer.

Image Loading
Image Loading

 

Step 3: Deploying the class library to GAC

1. Open the command prompt
2. Change directory to Program Files\Common Files\ Microsoft Shared\ web server extension\12\Bin

 

Image Loading

3. Type the command gacutil /I complete path of the dll of class created as eventreceiver class in step1

Gacutil /I [Complte path of dll of class]

Image Loading

The above command will deploy the dll in Gac assembly.

Step 4: Deploy the Event Receiver assembly

This step could be done I either in three ways

1. Through code
2. Through Stsadm command ( Features)
3. Through the content types

I am going to deploy using the program. 

1. Either create another project of console type or add class to any existing project.
2. Add the reference of SharePoint dll.
3. Return the Site Collection using SPSite.
4. Return the top level site using SPWeb.
5. Return the list using SPList.
6. Add the event in the list using SPEventRecieverDefinition.
7. Dhan is the name of the list which event I am going to capture.
8. Class name is the name of the class, you created in step1.
9. Assembly is the assembly information of the dll in GAC. To get information about dll in GAC follow below steps.
a. Go to Start -> Run
b. Type Assembly

 

Image Loading

c. Window will open; seek for dll you added.
d. Right click there and go to properties for all the information.

Image Loading

10. In Type select ItemDeleting.
11. Then call the update method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ConsoleApplication1
{class Program
    {static void Main(string[] args)
        {

            SPSite spsite = new SPSite("http://adfsaccount:2222/");
            SPWeb mysite = spsite.OpenWeb();

            SPList theList = mysite.Lists["Dhan"];
            SPEventReceiverDefinition newReceiver = theList.EventReceivers.Add();
            newReceiver.Class = "eventhandlersampleclasslibrary.Class1";
            newReceiver.Assembly = "eventhandlersampleclasslibrary,Version=1.0.0.0,Culture=neutral,PublicKeyToken=fd7c0a9671b2ae14";
            newReceiver.SequenceNumber = 5000;
            newReceiver.Type = SPEventReceiverType.ItemDeleting;
            newReceiver.Update();
            Console.Read();
        }
    }
}

Now the event has been deployed.

Output

Image Loading

When I tried to delete the item

Image Loading

I got the below error message; 

Image Loading

Conclusion

I have shown how to handle item deleting event. Whenever any item will get deleted user will get error message. Thanks for reading.Happy Coding

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top