Wednesday, April 6, 2011

How to create Item Event Receiver (SPItemEventReceiver)

Another piece of work again. L.. No worries.. would do this in no time. I will be very short and just focus on code. While creating this Event Handler I have used only 3 files.

  1. Feature.xml
  2. Elements.xml
  3. Event Handler class (event handler class inhering from SPItemEventReceiver)

Let us how the code works

Feature.xml
The feature file will remain very simple, only with the basic details.

<?xml version="1.0" encoding="utf-8"?>
<Feature Id="62B66824-3AE2-4320-9949-6B59ED862C64"
Title="TestEventHandler"

Description="Adding/updating/deleting of any item in list"

Version="1.0.0.0"

Hidden="FALSE"

Scope="Web"

DefaultResourceFile="core"

ImageUrl ="NEWSPG.GIF"

xmlns="http://schemas.microsoft.com/sharepoint/">

<
ElementManifests>
<ElementManifest Location="elements.xml"/>
</ElementManifests>
</Feature>

The feature will be very simple and straight forward, nothing special about this except mentioning the element manifest. 

Elements.xml
The elements.xml will hold all the receiver information. The handler assembly and class information would be placed here to execute the events.

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns=http://schemas.microsoft.com/sharepoint/>
<Receivers ListTemplateId="100">
<Receiver>
<Name>AddingEventHandler</Name>
<Type>ItemAdded</Type>
<SequenceNumber>10000</SequenceNumber>
<Assembly>TestEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ab011vdd6a7bfaba2</Assembly>
<Class>TestEventHandler.TestEventHandler</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
<Receiver>
<Name>UpdatedEventHandler</Name>
<Type>ItemUpdated</Type>
<SequenceNumber>10000</SequenceNumber>
<Assembly>TestEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ab011vdd6a7bfaba2</Assembly>
<Class>TestEventHandler.TestEventHandler</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
<Receiver>
<Name> Delete</Name>
<Type>ItemDeleting</Type>
<SequenceNumber>10000</SequenceNumber>
<Assembly>TestEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ab011vdd6a7bfaba2</Assembly>
<Class>TestEventHandler.TestEventHandler</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</Receivers>
</Elements>

Here on any item added/updated/deleted the event will fire in the class mentioned below <Class>TestEventHandler.TestEventHandler</Class> would handle the events.

Even Handler Class 

Event handler class would inherit the base class and contain the custom code under.....

public class TestEventHandler: SPItemEventReceiver

public override void ItemAdded(SPItemEventProperties properties)

public override void ItemUpdated(SPItemEventProperties properties)

The SPItemEventReceiver class is not instantiated but the item event receiver class of a custom event handler must derive from this class and override its methods for the event types that are handled.

namespace Example_Namespace

{

public class Class_Name : SPItemEventReceiver

{

public override void ItemAttachmentAdded(SPItemEventProperties properties)

{

using (SPSite oSiteCollectionEvent = new SPSite(properties.SiteId))

{
SPWeb oSiteEvent = oSiteCollectionEvent.OpenWeb(properties.RelativeWebUrl);
SPListItemCollection oItemsEvent = oSiteEvent.Lists[properties.ListTitle].Items;
}

using (SPSite oSiteCollection = new SPSite("http://Top_Site"))
{
SPWeb oWebsite = oSiteCollection.OpenWeb("Website_Name");

SPList oList = oWebsite.Lists["Announcements"];

SPListItemCollection collListItems = oList.Items;

SPListItem oItem = collListItems.Add();

oItem["Title"] = properties.UserDisplayName + " added an attachment to " + oItemsEvent[properties.ListItemId].Title + " in list " + properties.ListTitle + " at " + properties.WebUrl;

oItem.Update();

} } } }

HaPpY CoDiNg... (Aurum)

No comments:

Post a Comment