How to: Create an Event Handler Feature with VSeWSS1.3
Prerequisites:
Read the following to understand about Event model and Feature in SharePoint.
Software Environment:
· VM with MOSS 2007 SP2
· Visual Studio 2008 Professional
Object Model:
SharePoint Object Model exposes several event classes inherited from Microsoft.SharePoint assembly. There are four important classes:
Building the Feature:
It shows how to add a simple event handler that prevents items from being added to a list. Two procedures are involved in this task:
· Creating an event handler in Microsoft Visual Studio
· Adding the event handler as a Feature in Windows SharePoint Services
1. Create a new project in Visual Studio by clicking File, pointing to New, and then clicking Project.
Select Empty project from the SharePoint template list and give a name to the project
2. Select GAC from the Trust Level ( Event Handlers must be deployed only in GAC)
3. In Solution explorer, Right click on the project, and Add a new Item to the project
Select SharePoint category and select the ‘event receiver’ template.
Select ‘Document Library’ from the list
4. This would have created set of files for ItemEventReceiver and ListEventReceiver, as shown below. Depending on the situation you can delete either one of the EventReceiver.
5. Open the ItemEventReceiver.cs file and add code to the ItemAdding event as shown below. The code will prevent adding new item to the list.
6. Similarly add code to the ListEventReceiver as shown below. The sample code will prevent adding new column to the list.
7. Select Project Properties and enter the URL location of website where you want to deploy the EventReceiver.
8. Next step is to create solution to deploy these two Features. Click on ‘WSP View’ (or press Ctrl+W,I), you can see list of files required for the Solution with two Features.
9. Open the ‘feature.xml’ under ItemEventReceiver and change the title to meaningful one. If required add ‘Description’ also.
10. DO the same for the other Event receiver.
11. Press F5 to compile and deploy the solution. Also this would create a WSP file under bin/debug directory. You can use this WSP file to deploy this in different website.
12. Go to SharePoint website, SiteActions->Site Settings->Site Feature and check that the two Features are deployed and activated properly.
13. Open the Document Library within the website and try to add an Item / a column. This should generate a custom error like this.
0 comments:
Post a Comment