Click here to Skip to main content
Email Password   helpLost your password?

Overview

Applies To

Summary

Microsoft provides ways to extend the out of the box functionality of SharePoint Portal Server 2007 e.g. Features, SharePoint Designer 2007, Windows Workflow Foundation etc. Features offer flexibility in terms of developing extended functionality � such as Page Templates, List, Content Types, Web Parts, Workflow and events � to new and existing SharePoint 2007 sites.

Contents

Introduction

The Feature Framework has been extended to allow developers to create custom Features. Features can be deployed by using SharePoint Portal Server 2007 new form of deployment, namely Solution Deployment. Solutions are custom packages (e.g. WSP file) or redistributable CAB files, created by developers and deployed by SharePoint Administrators. Administrator can deploy Features to the individual site or to all Web front End Servers.

In this article, I will walk-through Creating and deploying an Event Handler Feature, which can be activated on the Web or Site, Site Collection, Web Application or Farm Level.

Components of Feature

A Feature can include any number of files, but it must include a Feature.xml file. The Feature.xml file, or Feature manifest, is the driver of the Feature, and this is the first file SharePoint looks at when invoking a Feature.

Features are organized in folders under the Features directory located under 12 hives; Where SharePoint Server 2007 puts all of its system files, at the following path: %SystemDrive%\Program Files\Common Files\Microsoft Shared\web server extensions\12.

In addition to the Feature.xml file, Features can include sub-folders and supporting files, such as element files that include for example, event handler references, ASPX pages deployed as part of the Feature, ASCX files, and DLL and RESX files.

Event Handler Feature

SharePoint Event Handler is a program that enhances and adds functionality throughout SharePoint List, List Items or Sites. Event Feature can be deployed to new or existing sites by using Features. SharePoint Object Model provides several event classes that can target event handlers to List, List Item or Sites.

Synchronous and Asynchronous Events

In addition to asynchronous events, SharePoint Server 2007 introduces synchronous events i.e. events that activate before the action occurs. Synchronous events can trap an item, document library or site before it is deleted. Synchronous events removed the ability for users to delete an item from the document library or event restricts users to delete columns from the document library.

SharePoint Object Model exposes several event classes inherited from Microsoft.SharePoint assembly. There are three main event classes:

Each class includes both synchronous and asynchronous methods to work with Item, List or Web Level. In this article, I will demonstrate how to hook an ItemDeleting, FieldDeleting or SiteDeleting Event to restrict users from deleting an item from the document library or from deleting a column from the document library.

SPItemEventReceiver

By overriding a member of the SPItemEventReceiver class, developers can restrict users from deleting, updating an Item in a List. Some of the important SPItemEventReceiver class members are described below:

Event Event Type Description
ItemAdded Asynchronous After an Item added in a list
ItemAdding Synchronous Before an Item added in a list
ItemAttachmentAdded Asynchronous After an attachment added from a list item
ItemAttachmentAdding Synchronous Before an attachment added from a list item
ItemAttachmentDeleted Asynchronous After attachment deleted from a list item
ItemAttachmentDeleting Synchronous Before attachment deleted from a list item
ItemCheckedOut Asynchronous After an item checked-In in a list
ItemCheckingOut Synchronous Before an item is checked out in a list
ItemCheckedIn Asynchronous After an item is checked-In in a list
ItemCheckingIn Synchronous Before an item is checked-In in a list
ItemDeleted Asynchronous After an item is deleted from a list
ItemDeleting Synchronous Before an item is deleted from a list
ItemUpdated Asynchronous After an item is updated in a list
ItemUpdating Synchronous Before an item is updated in a list

For the demonstration, I am using a synchronous event i.e. ItemDeleting, which restricts users from deleting an item in a list (document library). You can invoke this event with any type of list of document library by specifying the ListTemplateId attribute in a Feature Schema file e.g. ItemEventReceiver.xml.

See the screenshot below of ItemEventReceiver.cs and ItemEventReceiver.xml for defining the ListTemplateId and Assembly.

ItemDeletingEvent.JPG
ItemEventReceiver.cs class

ItemDeletingEvent.JPG
ItemEventReceiver.xml file

The following table shows the default list template integer IDs that you can use in defining the template ID for event handlers.

List Template Id List Template
100 Generic list
101 Document library
102 Survey
103 Links list
104 Announcements list
105 Contacts list
106 Events list
107 Tasks list
108 Discussion board
109 Picture library
110 Data sources
111 Site template gallery
113 Web Part gallery
114 List template gallery
115 XML Form library
120 Custom grid for a list
200 Meeting Series list
201 Meeting Agenda list
202 Meeting Attendees list
204 Meeting Decisions list
207 Meeting Objectives list
210 Meeting text box
211 Meeting Things To Bring list
212 Meeting Workspace Pages list
300 Portal Sites list
1100 Issue tracking
2002 Personal document library
2003 Private document library

SPListEventReceiver

By overriding a member of the SPListEventReceiver class, developers can restrict users from deleting, updating a list from a Site. Some of the important SPListEventReceiver class members are described below.

Event Event Type Description
FieldAdded Asynchronous After a document is added in a List
(Document Library)
FieldAdding Synchronous Before a document is added in a List
(Document Library)
FieldDeleted Asynchronous After a document is deleted from a list
(Document Library)
FieldDeleting Synchronous Before a document is deleted from a list
(Document Library)
FieldUpdated Asynchronous After a document is updated in a list
(Document Library)
FieldUpdating Synchronous Before a document is updated in a list
(Document Library)

For the demonstration, I am using a synchronous event i.e. FieldDeleting, which restricts users from deleting a document from a list (Document Library). In this case, I am again specifying ListTemplateId = 101 for Document Library in a ListEventReceiver.xml file.

SPWebEventReceiver

By overriding a member of the SPWebEventReceiver class, developers can restrict users from deleting a site from a site collection or even completely delete a site collection. Some of the important SPWebEventReceiver class members are described below:

Event Event Type Description
SiteDeleted Asynchronous After a site collection has been deleted
SiteDeleting Synchronous Before a site collection is being deleted
WebDeleted Asynchronous After a web site has been deleted
WebDeleting Synchronous Before a web site is being deleted

For the demonstration, I am using a synchronous event i.e. WebDeleting, which restricts users from deleting a web site from a site collection.

Building the Solution

We need Visual Studio 2005 to create a solution for providing the required functionality.

The steps required to create an event handler for SharePoint Sever application are described below:

Attributes of Feature.XML File

Some of the attributes of Feature Tag are described below:

Attribute Value Description
ID GUID Contains the globally unique identifier (GUID) for the Feature.
Title Text Returns the title of the Feature. Limited to 255 characters.
Scope Farm/WebApplication/Site/Web Can contain one of the following values: Farm (farm), WebApplication (Web application), Site (site collection), and Web (Web site).
Hidden True/False This attribute equals FALSE by default.
AlwaysForceInstall True/False Optional Boolean. TRUE if the Feature is installed by force during installation even if the Feature is already installed. For example, if set to TRUE, Feature installation callouts will always fire anytime a user tries to install the Feature (even if it is already installed) by using either the scanforfeatures or installfeature command-line operation. This attribute equals FALSE by default. The AlwaysForceInstall attribute affects Features of all scopes.

See screenshot below of Feature.XML file.

FeatureXML.JPG

Note: Complete source code of solution and package file is attached with this article.

Packaging the Features

In SharePoint Server 2007, Solutions are used to package and deploy Features, Site Definitions, Web Parts, Template Files, Assemblies and Code Access Security (CAS) policies to SharePoint front-end servers.

Steps for creating a Solution Package are described below:

Make WSP Cabinet File.JPG

Deploying the Features

Steps for deploying a Solution Package are described below:

add solution.JPG pg1.JPG

See screen below for a list of solutions deployed in a farm.

pg2.JPG pg3.JPG pg3.JPG

Activating the Solution

pg6.JPG Activate.JPG

Testing the Solution

Navigate to a Document Library in a Site, Where you have activated ItemEventResource feature.

pg8.JPG

Navigate to a Document Library in a Site, Where you have activated ListEventResource feature.

pg7.JPG

Navigate to a Site in a Web Application, Where you have activated WebEventResource feature.

Deactivating and Uninstalling the Solution

You can choose to turn deactivate a particular feature on your site or web application, or you can choose to uninstall a feature, which completely removes the feature from your SharePoint deployment. Features can be deactivated either through the administrative user interface or by using the STSADM.EXE command-line tool.

deactivate.JPG

Note: Files deployed as part of the Feature, such as ASPX, ASCX files are not removed when a Feature is uninstalled.

Platforms Tested

I have tested the included project on the following platforms:

References

Conclusion

In the article, I have demonstrated how to work with Features to enhance and extend SharePoint Sites by creating features for restricting users from deleting item, column and site for web application. This article also covered Features Packaging and deployment option and how can you de-activate or uninstall an existing feature.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralDeployment
Aneesur Rehman Khan
6:52 11 Feb '10  
Hi,

Please use WSPBuilder to easily package and deploy solution, and this tool make deployment very easy.
It deactivate and retract solution whenvere required and so no need to use stsadm and visit Central administration site.Farm Feature can be deployed very easily.

Thanks,
Anees
AnswerCreate Custom SharePoint Features, it is simple.
itsaranga
18:34 29 Nov '09  
Custom SharePoint features and more ..!
Generalproblem with handling page events
abdul1
1:41 10 Nov '09  
hi,

i have a news site (variation implemented). i want to capture page update event as i have to send the same content with some addtions to different email addresses when the page is published.

the problem is that i am not able to capture event any event of those news pages while able to capture events of other lists. i have set ListTemplateId="101".

any clue
QuestionUpdate or extended the existing Feature
Member 4750963
7:39 7 May '09  
It's realy a good article.
I have a quick question. In what scenario you will go for extending the feature with different version OR you will change the feature Id. Considering your feature is in live and you want to do something in your existing feature.

If yes then how? can you please explain it with a bit example.

Thanks in Advance.
GeneralError for sitedelete
mstabb
7:15 11 Mar '09  
Hi Syed

I am recieving the following error when trying to activate the "Restrict Deletion of Site from a Site Collection" feature.
The other two work fine with no errors.

A feature 'Receivers' element must have attribute 'ListTemplateId'. at Microsoft.SharePoint.SPEventElement.Initialize()
at Microsoft.SharePoint.SPEventElement.ElementActivated(SPFeaturePropertyCollection props, SPSqlCommand sqlcmdAppendOnly, SPWebApplication webApp, SPSite site, SPWeb web, Boolean fForce) etc etc etc

Just so you know I am using VS2008 etc.

Everyting else works fine and I have made some alteration, additions quite successfully despite being a c# novice ported over from VB.net

On a positive note I really like your style and find your articles most infomative and logical to follow.

Thank you for your efforts.

Mark

mstabb

Generalunable 2 delete the site.
shamsiya
16:44 10 Mar '09  
Hi,

Im a beginner in sharepoint and Im unable 2 activate this feature ' Restrict Deletion of Site from a Site Collection ' by your article . error is we have 2 provide listtemplateid.but in other articles its mentioned as its optional. its giving the same error again.if anybody knows about this error pls let me know.
GeneralCorrection
Jose Pablo Vega
7:29 24 Dec '08  
This is a small error, but it might confuse some people.

On the article's 1st table, the mistake is on this row:
-------------------------------------------------------------------------
ItemCheckedOut | Asynchronous | After an item checked-In in a list
-------------------------------------------------------------------------

"checked-In" should be "checked-Out".

BTW, great article.

-JP
QuestionEvent not firining
TheYoda
21:01 12 Nov '08  
Hi!

Im using the ItemAdded(SPItemEventProperties properties) to detect when a new document is uploaded to a library. It works like a charm, but when i create a new document (the new button) nothing happens? Why?

I must have an event on both because im renaming the documnets in the library... Any ideas?
QuestionFeature not found in the sitecollection feature
raajii
5:58 22 Oct '08  
I have a question, should i include the feature.xml and EventHandlerReceiver.wsp in ..\\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES folder?

If it so how to do that? Whether i should create any folder named EventHandlerReceiver inside that, the path I mentioned earlier.

and whether i need to put the EventHandlerReceiver.dll in to GAC?
GeneralHow can use with itemadded for a particular document library
sonashish
9:51 2 Oct '08  
Hello

This is great article to understand the concept of event handler. I am trying to understand How Can I implement same code for itemadded for particular document library i.e. "My Docs".

Any idea D'Oh!

Ashish
GeneralExcellent article
Member 3634250
11:05 19 Aug '08  
great effort
GeneralRe: Excellent article
Portatofe
9:38 2 Oct '08  
Big Grin


GeneralProject Server 2007 Event Handler
henryoh
6:55 16 Jul '08  
I want to automate an Event Handler for Project Server 2007. Do you know of a method that I can deploy using a feature it is set up a bit different?


Thanks,

H
GeneralHi, May I ask: SharePoint EventHandler Deployed as Solution + Feature(.wsp or .wsp.cab), cannot deploy, no resources, and cannot trigger EventHandler Code. Thank you!
qurl2008
17:12 10 Jul '08  
Hi Blogger and friends,

I have a problem on SharePoint EventHandler deployed as .wsp.
Now I`m on my way to “Solution + feature”, and its deployment depends on .wsp or.wsp.cab file. I almost reach the final step, except for one problem.

I use tool to help feature deployment, which is STSDEV(has WPSBuilder quick-start function embodied). It builds automatically a good files structure in the solution folder, and has a series of function like “WSPBuilder, Deploy, to 12 hive, etc.”

My problems lays on here:

1. Can addsolution, but cannot deploysolution whenever I add -url parameter.

2. For hundreds of time, Given that I use feature deployment way, the eventHandler never inspire, never work nor be able to debug steply.

Why my .dll works and debug well when pragramatically deploy and so pecimistically lousy by the feature approach?

Wish to get your kind instructions, thank you again!!

Bin> stsadm -o addsolution -filename “D:\NickTest160\NickTest.wsp” (Successful!)
stsadm -o execadmsvcjobs
stsadm -o deploysolution -name NickTest.wsp -url http://localhost -immediate -allowgacdeployment -force (On This step I Fail! SDK Command Line following gave me such notice:
“This solution contains no resources scoped for a web application and cannot be deployed to a particular web application.”

I tried again, by removing the -url parameter, and succeed to pass, why? Should one feature be deployed on some level of web, or list?

mnn, run it, and EventHandler still doesn`t inspire.. My heart sink.

My Feature ID, Solution ID, all .xml files has been carefully overlooked, seems good enough. But Why Lack of Resources? is it that Something more must be embodied in .wsp.cab compact file? I uncompressed it and see, there are:
NickTest180.dll
manifest.xml
(Folder)OssurEventHandler {feature.xml; elements.xml}

I looked into relative materials on web, but failed to find the solution. Really hope you help me. Thank you!!

Best Regards.

Nick.

Nick

QuestionDeactivating and uninstalling features [modified]
Lucy
5:29 10 Mar '08  
What are the risks if you uninstall a feature without deactivating it first? Why must you deactivate feature before uninstalling it?

Lucy

modified on Monday, March 10, 2008 10:58 AM

GeneralVery Good Effort adnan
Tariq Younas
0:59 26 Feb '08  
Really very good effort by Adnan.

Regards,
Tariq Younas
Software Engineer
SharePoint Developer
INTECH PROCESS AUTOMATION

Generalcan we customer listview with new webpart containing paging
sanong
16:11 18 Nov '07  
I have customer with design issue where they have big lists in one page hence the page slowly display. We need to have listview webpage with paging so that page can display faster.

any advise

GeneralTrap event When a role is created
Saisreenath
1:09 14 Nov '07  
Hi,

I wud like to know if there is a way to trap event when a role is created...how do i do it

thanks ,
Sai





sreenath

GeneralExcellent article
Aziz ur Rahman Syed
22:43 13 Nov '07  
Excellent article

Syed Aziz ur Rahman

GeneralHow to Trap the folder creation event
Saisreenath
19:09 18 Oct '07  
hi guys,

im using item added event to trap when a file is added in document library.... it works fine.....


i wud like to trap the event when a new folder is created in sharepoint....how do i do this??

thanks
Sai

sreenath

GeneralKeep up the good work - Adnan
boura
18:59 12 Oct '07  
Very nice article. It helped me a lot. Thanks
GeneralGreat article but please resolve this
powerkru
23:31 11 Oct '07  
When I activate webevent receiver I get an error that the Receivers element needs an ListTemplateId which is not provided in the example. I'm a beginner in Sharepoint programming, am I missing any thing



Thanks for ur Efforts
Powerkru
GeneralSharepoint portal
Brave_Gass
22:53 4 Oct '07  
Where to write code in sharepoint designer?
Where should I add server side code(code behind C# code) on the page in sharepoint designer(MOSS 2007)


Thanks
Prakash
QuestionPut the user profile properties " Departament " in eventhandler
deharf
13:15 26 Sep '07  
Hi

is it possible to insert the user perfile peroperties en the list with column name Departament ej: Departament->Informatique in de event handler "ItemAdding"

i make this code but not work !!!!

public override void ItemAdding(SPItemEventProperties properties)
{
SPSite mySite = new SPSite(properties.Context.ToString());
ServerContext myContexto = ServerContext.GetContext(mySite);
UserProfileManager myManPerfil = new UserProfileManager(myContexto);

UserProfile myUsuario = myManPerfil.GetUserProfile(properties.UserLoginName);


if (myUsuario["Department"].Value.ToString() != null)
{


properties.AfterProperties["Departament"] = myUsuario["Department"].Value.ToString();
}

else
properties.AfterProperties["Departament"] = "???";
}



but this work


public override void ItemAdding(SPItemEventProperties properties)
{


properties.AfterProperties["Departament"] = "Informatique"

properties.AfterProperties["Departament"] = "???";
}




thank you


Farid Dehar
GeneralHi good article - and I need your advise
sanong
6:41 8 Sep '07  
We have performance problem with FlatList in sharepoint.

On a page we have multiple lists with
-some of items contain calcalted field
-list contain 100 or 1000 items

immagin that when we click a page then it give you blank page for a while and come back with a list view. if you go forward and back again it will repeat again.

I am looking for a customise list webpart with better performance.

Any one pls.

sanong
Unsure


Last Updated 18 Nov 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010