Skip to main content

Implementing Event Handler in MOSS

How to implement Event Handler Feature in sharepoint and how to activate it ?
Below example shows how to add a simple event handler that prevents items from being deleted from a list. Two procedures are involved in this task:
  1. Creating an event handler in Microsoft Visual Studio
  2. Adding the event handler as a Feature in SharePoint
To create the event handler in Visual Studio
  1. Create a new project in Visual Studio by clicking File, pointing to New, and then clicking Project.
  2. In the New Project dialog box, select Visual C# in the Project types box, select Class Library in the Templates box, type DeletingEventHandler in the Name box, and then click OK.
  3. In Solution Explorer, select DeletingEventHandler, and click Add Reference on the Project menu.
  4. In the Add Reference dialog box, select Microsoft.SharePoint on the .NET tab and then click OK.
  5. In the Code Editor, import the Microsoft.SharePoint namespace as follows.
Change the name of the class like DeleteItemEvent and Inherit it from  the SPItemEventReceiver class, as follows.

public class DeleteItemEvent : SPItemEventReceiver

Now add code within the class to override the ItemDeleting method given Belowe.

public class DeleteItemEvent : SPItemEventReceiver{
    public override void ItemDeleting(SPItemEventProperties properties)
    {
        properties.Cancel = true;
        properties.ErrorMessage = "Deleting item from" + " " + properties.ListTitle + " " + "is not supported";
    }
}



Now go to Project menu in visual studio, in this last option is DeleteItemEvent Properties click the Signing tab, select Sign the assembly, select Choose a strong name key file, and then click <New...>. 

In the Create Strong Name Key dialog box, type DeletingEventHandler.snk in the Key file name box, optionally specify a password for the key, and then click OK.

To build the project, click Build Solution on the Build menu, or press CTRL+SHIFT+B.

Find the \DeletingEventHandler\bin\Debug folder in the Visual Studio Projects folder, and drag the DeletingEventHandler.dll file to Local_Drive:\WINDOWS\assembly to place the DLL in the global assembly cache.

2.To add the event handler as a Windows SharePoint Services Feature

  1. Create a folder in Local_Drive:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES called DeletingEventHandler.
  2. Create a Feature.Xml file in this folder like the following that identifies the Feature and its element manifest file and sets the Feature scope to Web site.
<Feature Scope="Web"     Title="Deleting Event Handler"     Id="GUID"     xmlns="http://schemas.microsoft.com/sharepoint/">
    <
ElementManifests>
    <
ElementManifest Location="Elements.xml"/>
    </
ElementManifests>
</
Feature>

Here generate a GUID by running guidgen.exe located in Local_Drive:\Program Files\Microsoft Visual Studio 8.

The Elements.xml file is given below. It is referenced in Feature.xml file in ElementManifest Location tag.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<
Receivers ListTemplateId="101">
    <
Receiver>
    <
Name>DeletingEventHandler</Name>
    <
Type>ItemDeleting</Type>
    <
SequenceNumber>22500</SequenceNumber>
    <
Assembly>ItemAddedEvent, Version=1.0.0.0, Culture=neutral,
    PublicKeyToken=d6009c72158cb154</Assembly>
    <
Class>ItemAddedEvent.ItemAddedEvent</Class>
    <
Data></Data>
    <
Filter></Filter>
    </
Receiver>
</
Receivers>
</
Elements>


Here in Name tag you have to give DLL name. In assembly change info of same for that. Find DLL in GAC and right click on DLL and click on property in that give all the information related to assembly.

In Class tag you have to give Namespace.classname.
In the first tag Receivers "ListtemplatedId" means on which list you have to perfom the Event Handler like Document Library, Custom List, PictureLibrary...etc. Here 101 means Document Library. List of ListTemplated Id are given below.
  • 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
  • 112   User Information list
  • 113   Web Part gallery
  • 114   List template gallery
  • 115   XML Form library
  • 116   Master pages gallery
  • 117   No-Code Workflows
  • 118   Custom Workflow Process
  • 119   Wiki Page library
  • 120   Custom grid for a list
  • 130   Data Connection library
  • 140   Workflow History
  • 150   Gantt Tasks 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
  • 301   Blog Posts list
  • 302   Blog Comments list
  • 303   Blog Categories list
  • 1100   Issue tracking
  • 1200   Administrator tasks list
In command prompt, navigate to \Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN on the local drive, and type each of the following commands to install the Feature in the deployment, activate the Feature on a specified subsite, and reset Microsoft Internet Information Services (IIS) so that the changes take effect:
  1. stsadm -o installfeature -filename DeletingEventHandler\Feature.xml
  2. stsadm -o activatefeature -filename DeletingEventHandler\Feature.xml -url http://Server/Site/Subsite
  3. iisreset
Try to delete a Document in Document Library on the specified Web site to see the effect of the event handler feature. Output will look like this.

Comments

Popular posts from this blog

Tab Control in Asp.Net

Scenerio: I need your help in designing tab control in   asp.net .My requirement is I need a tab control in   asp.net (C#) like  for example goto my computer ,right click c drive and select properties.we get tabs like general,security etc....... like that i need to design one tab control(no need of any rightclick) in my web page and the database table columns should come as tabs and inseide the tab data of that particular column should come.   Example:Employee master tab1:Employee name.........his name in the tab tab2:Age.............his age tab3:Address........his address   Solution:   You can do this using a simple div <style type="text/css"> .tabs         {             position: relative;             height: 20px;             margin: 0;   ...

AI and Microsoft: Revolutionizing Efficiency in Nonprofit Organizations

  How AI and Microsoft Enhance Efficiency in Nonprofit Organizations In today’s fast-paced world, nonprofit organizations face unique challenges—limited resources, increasing demands, and the constant need to do more with less. But what if technology could be the game-changer they need? In my latest research paper,  "How AI and Microsoft Enhance Efficiency in Nonprofit Organizations" , I explore how cutting-edge technologies like Artificial Intelligence (AI) and Microsoft’s innovative tools are revolutionizing the way nonprofits operate. From streamlining administrative tasks to enhancing donor engagement and optimizing resource allocation, AI and Microsoft’s solutions are empowering nonprofits to focus on what truly matters—their mission. This paper dives deep into real-world examples, practical applications, and the transformative potential of these technologies. Whether you’re a nonprofit professional, a tech enthusiast, or simply curious about the intersection of technolo...

Social tagging overview in Sharepoint 2010

A tag is a word or phrase that identifies an individual piece of information according to a set of attributes or criteria. Tags make it easy to find and share information about a specific subject or task. Social tagging helps users categorize information in ways that are meaningful to them. Social tagging can improve the quality of search results by filtering against specific tags, and it can also connect individuals who want to share information with other users who have like interests. This article describes the social tagging features in Microsoft SharePoint Server 2010. This article does not describe how to configure social tagging features. It also does not discuss how to implement social tagging features as part of an overall social media strategy for an enterprise. About using social tagging features Social tagging features help users to share information and to retrieve relevant, high-quality content more efficiently. Such sharing encourages collaboration and b...