In SharePoint 2010 the Event Receivers has been improved so much. But there is still a big issue on the default functionality when we want to attach/register an event receiver to a single list. By default when we create an event receiver project Visual Studio creates an Elements.xml associated to the event receiver that contains the association xml generated from the information that the Visual Studio wizard asked us. Basically, this xml associates the event receiver to a content type that we specified.
So, how should we attach an event receiver to a single list? We have two options the create a little application and do it through the object model, or using the PowerShell scripting equivalent to the object model code.
Let’s go through the second option: PowerShell
The first thing that we should do is to deploy the assembly of the Event Receiver into the GAC.
After that we can create and execute our PowerShell script.
Here It’s the sample script to register an ItemUpdated List Item Event Receiver to a single list/library:
=============================================================
$spList
=
$spWeb
.Lists[
"My List Name"
]
$spEventReceiver
=
$spList
.EventReceivers.Add()
$spEventReceiver
.Assembly =
"Project.Name.Class, Version=1.0.0.0, Culture=neutral, PublicKeyToken=24242342424"
$spEventReceiver
.Class =
"Namespace.MyClass.ClassName"
$spEventReceiver
.Name =
"My Event Name"
$spEventReceiver
.
Type
= 10002
$spEventReceiver
.SequenceNumber = 1000
$spEventReceiver
.Synchronization = 1
$spEventReceiver
.Update()
=========================================================
And some explanation of each property:
- We can get all the assembly and class information with .Net Reflector from our .dll.
- What name to put in the Name property is your choice.
- The Type property is an enumerated property whose possible values are listed in the table 1.
- The SecuenceNumber property determines the execution order of this event receiver when there are more than one event receiver attached to the same list.
- The Synchronization property is another enumerated field that could have three possible values, listed in the table 2.
Finally, we should call the Update method to persists the changes.
Well, after add our event we can check which events are attached to a list with the next PowerShell script:
======================================================
$spList
=
$spWeb
.Lists[
"My List Name"
]
$spList
.EventReceivers |
Select
Name,Assembly,
Type
====================================================
This script just show us Name, Assembly and Type properties of each event attached to the list.
After that, maybe we want to delete any event receiver from a list. As you can guess… Yes! another PowerShell script can save us!:
====================================================
$spList
=
$spWeb
.Lists[
"My List Name"
]
$eventsCount
=
$spList
.EventReceivers.Count
$assembly
=
"Project.Name.Class, Version=1.0.0.0, Culture=neutral, PublicKeyToken=24242342424"
$class
=
"Namespace.MyClass.ClassName"
$type
= 10002
$name
=
"My Event Name"
for (
$i
= 0;
$i
-lt
$eventsCount
;
$i
+=1)
{
if (
$spList
.EventReceivers[
$i
].Assembly
-eq
$assembly
-and
$spList
.EventReceivers[
$i
].Class
-eq
$class
-and
$spList
.EventReceivers[
$i
].
Type
-eq
$type
-and
$spList
.EventReceivers[
$i
].Name
-eq
$Name
)
{
$spList
.EventReceivers[
$i
].Delete()
}
}
$spList
.Update()
=========================================================
Cool, isn’t it? In the above script we go through all the events associated to a list and delete those which Assembly, Class, Type and Name values are equal to the variables values that we set first.
As I promised here are the tables with the enumerator values.
Table 1
Member name Enum code Description
InvalidReceiver -1 Indicates that an invalid event receiver type has been specified.
ItemAdding 1 Event that occurs before an item has been added.
ItemUpdating 2 Event that occurs before an item is updated.
ItemDeleting 3 An event that fires before an item is deleted.
ItemCheckingIn 4 An event that occurs before an item has been checked in.
ItemCheckingOut 5 An event that occurs before an item is checked out.
ItemUncheckingOut 6 An event that occurs before an item is unchecked out.
ItemAttachmentAdding 7 Event that occurs before an attachment has been added to an item.
ItemAttachmentDeleting 8 An event that occurs before an attachment has been removed from the item.
ItemFileMoving 9 An event that occurs before a file is moved.
FieldAdding 101 Event that occurs before a field is added to a list.
FieldUpdating 102 Event that occurs before a field is updated.
FieldDeleting 103 An event that occur before a field is removed from a list.
ListAdding 104 Event that occurs before a list is created.
ListDeleting 105 An event that occurs before a list is deleted.
SiteDeleting 201 Event that occurs before a site collection is deleted.
WebDeleting 202 Event that occurs before a site is deleted.
WebMoving 203 Event that occurs before site a site URL has been changed.
WebAdding 204 An event that occurs before a new site is created.
WorkflowStarting 501 Event that occurs before a workflow starts running.
ItemAdded 10001 Event that occurs after an item has been added.
ItemUpdated 10002 Event that occurs after an item has been updated.
ItemDeleted 10003 An event that occurs after an item has been deleted.
ItemCheckedIn 10004 Event that occurs after an item has been checked in.
ItemCheckedOut 10005 An event that occurs after an item has been checked out.
ItemUncheckedOut 10006 An event that occurs after an item has been unchecked out.
ItemAttachmentAdded 10007 An event that occurs after an attachment has been added to the item.
ItemAttachmentDeleted 10008 Event that occurs after an attachment has been removed from the item.
ItemFileMoved 10009 An event that occurs after a file has been moved.
ItemFileConverted 10010 An event that occurs after a file is transformed from one type to another.
FieldAdded 10101 An event that occurs after a field has been added.
FieldUpdated 10102 An event that occurs after a field has been updated.
FieldDeleted 10103 An event that occurs after a field has been removed.
ListAdded 10104 Event that occurs after a list has been created.
ListDeleted 10105 Event that occurs after a list has been deleted.
SiteDeleted 10201 Event that occurs after a site collection has been deleted.
WebDeleted 10202 Event that occurs after a site has been deleted.
WebMoved 10203 Event that occurs after a site URL has been changed.
WebProvisioned 10204 An event that occurs after a new site has been created, but before that new site is provisioned.
WorkflowStarted 10501 Event that occurs after a workflow has started running.
WorkflowPostponed 10502 Event that occurs after a workflow has been postponed.
WorkflowCompleted 10503 An event that occurs after a workflow has completed running.
EmailReceived 20000 Event that occurs after a list receives an e-mail message.
ContextEvent 32766 Identifies workflow event receivers, and is therefore not a true event type.
Table 2
Member name Enum code
Default 0
Synchronous 1
Asynchronous 2
Tip: If we want avoid that the event receiver is registered to any content type, we should delete the Elements.xml from the VS project.