add/delete an event receiver to a single list with PowerShell in SharePoint 2010


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:
=============================================================

$spWeb = Get-SPWeb -Identity http://moss2010
$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:
======================================================

$spWeb = Get-SPWeb -Identity http://moss2010
$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!:
====================================================

$spWeb = Get-SPWeb -Identity http://web.spdev.local
$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 nameEnum codeDescription
InvalidReceiver -1Indicates that an invalid event receiver type has been specified.
ItemAdding 1Event that occurs before an item has been added.
ItemUpdating 2Event that occurs before an item is updated.
ItemDeleting 3An event that fires before an item is deleted.
ItemCheckingIn 4An event that occurs before an item has been checked in.
ItemCheckingOut 5An event that occurs before an item is checked out.
ItemUncheckingOut 6An event that occurs before an item is unchecked out.
ItemAttachmentAdding 7Event that occurs before an attachment has been added to an item.
ItemAttachmentDeleting 8An event that occurs before an attachment has been removed from the item.
ItemFileMoving 9An event that occurs before a file is moved.
FieldAdding 101Event that occurs before a field is added to a list.
FieldUpdating 102Event that occurs before a field is updated.
FieldDeleting 103An event that occur before a field is removed from a list.
ListAdding 104Event that occurs before a list is created.
ListDeleting 105An event that occurs before a list is deleted.
SiteDeleting 201Event that occurs before a site collection is deleted.
WebDeleting 202Event that occurs before a site is deleted.
WebMoving 203Event that occurs before site a site URL has been changed.
WebAdding 204An event that occurs before a new site is created.
WorkflowStarting 501Event that occurs before a workflow starts running.
ItemAdded 10001Event that occurs after an item has been added.
ItemUpdated 10002Event that occurs after an item has been updated.
ItemDeleted 10003An event that occurs after an item has been deleted.
ItemCheckedIn 10004Event that occurs after an item has been checked in.
ItemCheckedOut 10005An event that occurs after an item has been checked out.
ItemUncheckedOut 10006An event that occurs after an item has been unchecked out.
ItemAttachmentAdded 10007An event that occurs after an attachment has been added to the item.
ItemAttachmentDeleted 10008Event that occurs after an attachment has been removed from the item.
ItemFileMoved 10009An event that occurs after a file has been moved.
ItemFileConverted 10010An event that occurs after a file is transformed from one type to another.
FieldAdded 10101An event that occurs after a field has been added.
FieldUpdated 10102An event that occurs after a field has been updated.
FieldDeleted 10103An event that occurs after a field has been removed.
ListAdded 10104Event that occurs after a list has been created.
ListDeleted 10105Event that occurs after a list has been deleted.
SiteDeleted 10201Event that occurs after a site collection has been deleted.
WebDeleted 10202Event that occurs after a site has been deleted.
WebMoved 10203Event that occurs after a site URL has been changed.
WebProvisioned 10204An event that occurs after a new site has been created, but before that new site is provisioned.
WorkflowStarted 10501Event that occurs after a workflow has started running.
WorkflowPostponed 10502Event that occurs after a workflow has been postponed.
WorkflowCompleted 10503An event that occurs after a workflow has completed running.
EmailReceived 20000Event that occurs after a list receives an e-mail message.
ContextEvent 32766Identifies workflow event receivers, and is therefore not a true event type.
Table 2
Member nameEnum code
Default 0
Synchronous 1
Asynchronous2
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.