Retrieving a List of Event Receivers using PowerShell

Background Information:20140425-1.png

We wish to get a list of all event receivers associated with a specific list in our SharePoint environment using a PowerShell Script. Event receivers in the context of SharePoint are similar to what triggers are to SQL Server. They are blocks of logic that execute automatically in response to a specific event. In our case, these events are generated at the list level (new item added, item modified, item deleted, etc.). We want our PowerShell script to prompt the user to input the name of the list we wish to analyze, as well as the URL of the Web containing that list. In return, the script will loop through all event receivers associated with the specified list and will print the full list on the PowerShell console.

Event Receivers in the SharePoint Object Model:

In the SharePoint Object Model, you can access the list of associated Event Receivers to an object by calling the .EventReceivers property of a SharePoint artefact. This property will return a SPEventReceiverDefinitionCollection object that contains all event receivers definitions associated with the artefact. To get more information about the various receivers, our script will iterate through that collection and will retrieve information about the name of each Event Receiver, as well as the event they are associated with (ItemAdding, ItemUpdated, etc). Each SPEventReceiverDefinition object exposes a property named “Type” that tells us what event the receiver is associated with, as well as a Name property that lists the name given to each receiver.

Script:

The following PowerShell script will prompt the user to input the URL of a SharePoint Web and the name of a SharePoint list or library, and will print a report listing all Event Receivers associated with it.

# Prompts the user for parameters;

$webURL = Read-Host “URL of the Web”

$listName = Read-Host “Name of the list/library”

 

# Get a reference to the SharePoint web;

$web = Get-SPWeb $webURL

 

# Get a reference to the SharePoint list or library;

$list = $web.Lists[$listName]

 

foreach($eventReceiverDef in $list.EventReceivers)

{

    if($eventReceiverDef.Name -ne “”)

    {

                    $eventInfo = $eventReceiverDef.Name + ” – ” + $eventReceiverDef.Type

                    Write-Host $eventInfo -BackgroundColor Green -ForegroundColor Black

    }

}

 

$web.Dispose()

20140426-2.png

If you wish to remove an instance of an EventReceiverDefinition, simply get a reference to it, and call the Delete() method on it.

Leave a Reply

Your email address will not be published. Required fields are marked *