There are many resources explained about Event Receiver Handler in Sharepoint. Let's take the SPItemEventReciever as an example: (I was planning to synchronize two list in sharepoint using Workflow, but for item deleting/deleted, the item cannot trigger an WF, in this case I wrote code of event reciever)
Here I wrote what I did in my project.
Step 1. Write the class inherited from SPItemEventReciever.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace DeleteReference
{
public class DeleteItem: SPItemEventReceiver
{
public override void ItemDeleted(SPItemEventProperties properties)
{
SPSite site= new SPSite("http://myserver:3000/");
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList("/Jerry/Lists/Task2");
SPListItem item = list.Items.Add();
item["Title"] = "A";
//item["Title"] = properties.ListItem["Title"];
item.Update();
}
}
public override void ItemDeleting(SPItemEventProperties properties)
{
using (SPWeb web = properties.OpenWeb().ParentWeb)
{
SPList list = web.GetList("/Jerry/Lists/Task2");
SPListItem item = list.Items.Add();
item["Title"] =properties.ListItem["Title"];
//item["Title"] = properties.ListItem["Title"];
item.Update();
}
}
public override void ItemAdded(SPItemEventProperties properties)
{
SPListItem item = properties.ListItem;
this.DisableEventFiring();
item["Release"] = "AB";
item.Update();
this.EnableEventFiring();
/*SPSite site = new SPSite("http://myserver:3000/");
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList("/Jerry/Lists/Release1");
SPListItem item = list.Items.Add();
item["Title"] = "C";
//item["Title"] = properties.ListItem["Title"];
item.Update();
}*/
}
}
}
Step 2.
Use the strong named signing and register it in GAC.
Step 3.
Write the feature.xml and Element.xml file in the folder(Create DeleteReference folder under the FEATURES folder)
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\DeleteReference
feature.xml:
< Feature Scope="Web"
Title="Deleting Event Handler"
Id="8CDFAE41-98F9-4139-9546-A7C0B7D3C1FE"
xmlns="http://schemas.microsoft.com/sharepoint/">
< ElementManifests>
< ElementManifest Location="Element.xml"/>
< /ElementManifests>
< /Feature>
Element.xml
< Elements xmlns="http://schemas.microsoft.com/sharepoint/">
< Receivers ListTemplateId="107">
< Receiver>
< Name>DeleteReference< /Name>
< Type>ItemAdded< /Type>
< SequenceNumber>10000< /SequenceNumber>
< Assembly>DeleteReference, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef537f21ff0cf29f< /Assembly>
< Class>DeleteReference.DeleteItem< /Class>
< Data>< /Data>
< Filter>< /Filter>
< /Receiver>
< /Receivers>
< Receivers ListTemplateId="107">
< Receiver>
< Name>DeleteReference< /Name>
< Type>ItemDeleted< /Type>
< SequenceNumber>10000< /SequenceNumber>
< Assembly>DeleteReference, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef537f21ff0cf29f< /Assembly>
< Class>DeleteReference.DeleteItem< /Class>
< Data>< /Data>
< Filter>< /Filter>
< /Receiver>
< /Receivers>
< Receivers ListTemplateId="107">
< Receiver>
< Name>DeleteReference< /Name>
< Type>ItemDeleting< /Type>
< SequenceNumber>10000< /SequenceNumber>
< Assembly>DeleteReference, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef537f21ff0cf29f< /Assembly>
< Class>DeleteReference.DeleteItem< /Class>
< Data>< /Data>
< Filter>< /Filter>
< /Receiver>
< /Receivers>
< /Elements>
Notice the ListTemplateID is also easy to be found in internet resources, for example:
GenericList 100
DocumentLibrary 101
Survey 102
Links 103
Announcements 104
Contacts 105
Events 106
Tasks 107
DiscussionBoard 108
PictureLibrary 109
For us we want our feature register to a task list, so we use 107.
Step 4.
Install the features to the subsite where the list is:
pushd %programfiles%\common files\microsoft shared\web server extensions\12\bin
::stsadm -o deactivatefeature -filename DeleteReference\feature.xml -url http://myserver:3000/Jerry/List/Task1
::stsadm -o uninstallfeature -filename DeleteReference\feature.xml
stsadm -o installfeature -filename DeleteReference\feature.xml
stsadm -o activatefeature -filename DeleteReference\feature.xml -url http://myserver:3000/Jerry/List/Task1
iisreset
No comments:
Post a Comment