Feature in Sharepoint is nothing but a group of Sharepoint elements, that can help user to accomplish a particular task. This task like to prevent the deleting Item from particular type of List, creating the list Item, creating site pages…so on.
You can find many features with your installation of sharepoint. To know about those features go to the directory
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE
In that TEMPLATE directory you can find folder called Features. In this folder you have 137 Features like AnnouncementsList, contactlist… so on.
If you open any feature folder you can find two folders and one xml file.
Even you can create your own feature. I will explain how to create your own feature to handle some event later in this article.
EventHandlers are helpful to execute your own code whenever particular event occurred.
For example, if you want restrict the user to delete an Item from particular List, then you can dot it by creating EventHandler.
Create EventHandler:
Here I am creating the event handler to restrict the user to delete an item from your Contacts List.
To Create Event handler in Sharepoint
Open visual studio File-> New->Project-> select Class Library for C#
And enter name for project as EventHandlerExample
Rename the class as CancelContactListItem. Right click on solution Explorer and click on Add Reference . Browse for the Sharepoint dll file(i.e, Microsoft.Sharepoint.dll) and Imports the namespace for sharepoint for your code.
Inherits SPItemEventReceiver for CancelContactListItem and override ItemDeleting method.
The code to restrict user from deleting the contact list item look like below.
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; namespace EventHandlerExample { public class CancelContactListItem:SPItemEventReceiver { public override void ItemDeleting(SPItemEventProperties properties) { properties.ErrorMessage = "You cannot Delete Contact List Item."; properties.Cancel = true; } } }
Here you can observed that, you inherits the SPItemEventReceiver and overrides the ItemDeleting method.
We are displaying the ErrosMessage to user whenever he attempts to delete an Item and we assign properties.Cancel = true to restrict the deletion of an Item.
Until now we created the Event Handler to restrict the deletion of item. Now we need to create Feature to accomplish this task.
To create feature for this event handler, you need to add new folder to for this solution.
Right click on solution explorer and click Add and select Newfolder and name it as Template. And add new folder to this Templatefolder and name it as Features.
Add one more folder to this Features folder and name it as EventHandlerExample( same as your project name).
Now we need to add two XML files to this inner folder (for the EventHandlerExample folder just we created) and name as Feature.xml and Elements.xml.
The code for Feature.xml file look like below.
<?xml version="1.0" encoding="utf-8"?> <Feature Id="0F675CFD-0E7A-4fa9-BC90-B69314AF6F88" Title="EventHandlerExample" Description="EvenHandler which cancel the deletion of Contact List Item" Version="1.0.0.0" Scope="Web" Hidden="TRUE" DefaultResourceFile="core" xmlns="http://schemas.microsoft.com/sharepoint/"> <ElementManifests> <ElementManifest Location="Elements.xml"/> </ElementManifests> </Feature>
In the above code you have Feature tag to describe the feature. For feature Id, you need to generate new GUID. For this go to Visual studio Tools and select Create GUID, click on copy and exit. The copy item look like below
// {0F675CFD-0E7A-4fa9-BC90-B69314AF6F88}
IMPLEMENT_OLECREATE(<<class>>, <<external_name>>,
0x27a581c9, 0xe8f6, 0x4046, 0xb4, 0xb7, 0xb0, 0x49, 0x73, 0x67, 0xa7, 0xe2);
Remove all items except 0F675CFD-0E7A-4fa9-BC90-B69314AF6F88. and paste this code in Feature.xml file for the Feature Id.
You can find Title attribute(title of the feature) and Description(description about your feature).
Scope attribute, which defines scope for your Feature. Here scope is Web, you can have scope as site also.
At the last of Feature.xml file you can find ElementManifests tag which defines location Elements.xml for the feature.
Before going to the Elements.xml, you need to generate public key token for this project.
For this Right click on solution Explorer and select properties.
In the properties window select Signing.
Select the “sign the assembly” check box, in the drop down list select new.
Give the name for your key file .
Now save all Items and Build project to generate dll file for your project.
Open visual studio command prompt and type
gacutil /i C:\Projects\EventHandlerExample\bin\Debug\EventHandlerExample.dll
Execute above line to add assembly to the GAC(Global Assembly Cache).
Now we need public token key for this Assembly. For this I executed following command in the command prompt.
sn -T C:\Projects\EventHandlerExample\bin\Debug\EventHandlerExample.dll
Then you can get the public key token value. Copy and paste this key at some place.
Here I got the public key token as c23b446df2567fdb.
Now go to Elements.xml file. The code for this xml file look like below.
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Receivers ListTemplateId="105"> <Receiver> <Name>EventHandlerExample</Name> <Type>ItemDeleting</Type> <SequenceNumber>21111</SequenceNumber> <Assembly> EventHandlerExample, Version=1.0.0.0, culture=neutral, PublicKeyToken=c23b446df2567fdb </Assembly> <Class>EventHandlerExample.CancelContactListItem</Class> <Data></Data> <Filter></Filter> </Receiver> </Receivers> </Elements>
In the above code, you have Receivers tag. For this you have ListTemplateId attribute. We have 105 for this. Sharepoint defines the id’s for all Lists. For contact List id value is 105.
For the Receiver tage, you have several tag like Name(Name of the event handler)
Type — It defines handler type
SequenceNumber – Sharepoint gave the SequenceNumber’s for all Features. After 20000 we can give any value for this.
For Assembly tag, you need to give namespace of the EventHandler, version and PublicKeyToken(just we created, for me it is c23b446df2567fdb).
For Class you have to give namespace.classname, here EventHandlerExample.CancelContactListItem.
So, you are successfully created the all required Feature elements. Then you need to install feature and activate it for particular web.
For this I created one batch file to install and activate this feature as a batch.
Open notepad, paste below code
@SET TEMPLATEDIR="c:\program files\common files\microsoft shared\web server extensions \12\Template" @SET STSADM="c:\program files\common files\microsoft shared\web server extensions \12\bin\stsadm" Echo Copying files to TEMPLATE directory xcopy /e /y TEMPLATE\* %TEMPLATEDIR% Echo Installing feature %STSADM% -o installfeature -filename EventHandlerExample\feature.xml -force REM IISRESET Echo activating the feature %STSADM% -o activatefeature -filename EventHandlerExample\feature.xml -URL http://home-pt72rbbzr0
Save it as install.bat.
In the above code, to activate the feature you need to give youe sharepoint web url, for me it is http://home-pt72rbbzr0.
Add this file to you project and Rebuild your project.
After successfully creating the feature, just open install.bat. Then the Feature is installed and activated for given URL.
Now you need to test the feature, just you created. Open your sharepoint web(which you gave to activate the feature) and create Item for your Contact List. Then try to delete that Item, you will get Errors message as
You cannot Delete Contact List Item.
Troubleshoot issues with Windows SharePoint Services.