Add-SPEventReceiver

Adds an event receiver to a web, list, or content type.

Add-SPEventReceiver -Web <SPWebPipeBind> -Name <String> -Assembly <String> -ClassName <String> -Type <SPEventReceiverType[]> -Sequence <Int32> [-AssignmentCollection [<SPAssignmentCollection>]]

Add-SPEventReceiver -List <SPListPipeBind> -Name <String> -Assembly <String> -ClassName <String> -Type <SPEventReceiverType[]> -Sequence <Int32> [-AssignmentCollection [<SPAssignmentCollection>]]

Add-SPEventReceiver -ContentType <SPContentTypePipeBind> -Name <String> -Assembly <String> -ClassName <String> -Type <SPEventReceiverType[]> -Sequence <Int32> [-AssignmentCollection [<SPAssignmentCollection>]]

Parameters

1

ParameterRequiredTypeDescription
WebTrueSPWebPipeBind

The web to add the event receiver to.

The value must be a valid URL in the form http://server_name

NameTrueString

The name to give to the event receiver. The name has no significance but can be useful when later listing the event receivers.

AssemblyTrueString

The fully qualified assembly name containing the event receiver class to add.

ClassNameTrueString

The fully qualified class name of the event receiver to add.

TypeTrueSPEventReceiverType[]

The event type to add. The command does not validate that you are adding the correct type for the specified target or that the specified class contains handlers for the type specified.

SequenceTrueInt32

The sequence number specifies the order of execution of the event receiver.

AssignmentCollectionFalseSPAssignmentCollection

Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.

When the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.

2

ParameterRequiredTypeDescription
ListTrueSPListPipeBind

The list to add the event receiver to.

The value must be a valid URL in the form http://server_name

NameTrueString

The name to give to the event receiver. The name has no significance but can be useful when later listing the event receivers.

AssemblyTrueString

The fully qualified assembly name containing the event receiver class to add.

ClassNameTrueString

The fully qualified class name of the event receiver to add.

TypeTrueSPEventReceiverType[]

The event type to add. The command does not validate that you are adding the correct type for the specified target or that the specified class contains handlers for the type specified.

SequenceTrueInt32

The sequence number specifies the order of execution of the event receiver.

AssignmentCollectionFalseSPAssignmentCollection

Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.

When the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.

3

ParameterRequiredTypeDescription
ContentTypeTrueSPContentTypePipeBind

The name of the content type to return. The type must be a valid content type name; a valid content type ID, in the form 0x0123...; or an instance of an SPContentType object.

NameTrueString

The name to give to the event receiver. The name has no significance but can be useful when later listing the event receivers.

AssemblyTrueString

The fully qualified assembly name containing the event receiver class to add.

ClassNameTrueString

The fully qualified class name of the event receiver to add.

TypeTrueSPEventReceiverType[]

The event type to add. The command does not validate that you are adding the correct type for the specified target or that the specified class contains handlers for the type specified.

SequenceTrueInt32

The sequence number specifies the order of execution of the event receiver.

AssignmentCollectionFalseSPAssignmentCollection

Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.

When the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.

Detailed Description

Adds an event receiver to a web, list, or content type.

Copyright 2016 Falchion Consulting, LLC

> For more information on this cmdlet and others:

> http://blog.falchionconsulting.com/

> Use of this cmdlet is at your own risk.

> Gary Lapointe assumes no liability.

Examples

------------------EXAMPLE 1-----------------------

PS C:\> Get-SPWeb http://demo | Add-SPEventReceiver -Name "My Cool Event Receivers" -Assembly "Falchion.SharePoint.MyCoolProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08" -ClassName "Falchion.SharePoint.MyCoolProject.MyCoolEventReceiver" -Type "WebProvisioned","WebDeleted"

This example adds WebProvisioned and WebDeleted event receivers to the http://demo site.

------------------EXAMPLE 2-----------------------

PS C:\> Get-SPList http://demo/Lists/MyList | Add-SPEventReceiver -Name "My Cool Event Receivers" -Assembly "Falchion.SharePoint.MyCoolProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08" -ClassName "Falchion.SharePoint.MyCoolProject.MyCoolEventReceiver" -Type "ItemUpdating","ItemAdding"

This example adds ItemUpdating and ItemAdding event receivers to the http://demo/Lists/MyList list.

------------------EXAMPLE 3-----------------------

PS C:\> Get-SPWeb http://demo | Get-SPContentType -Identity "My Content Type" | Add-SPEventReceiver -Name "My Cool Event Receivers" -Assembly "Falchion.SharePoint.MyCoolProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08" -ClassName "Falchion.SharePoint.MyCoolProject.MyCoolEventReceiver" -Type "ItemUpdating","ItemAdding"

This example adds ItemUpdating and ItemAdding event receivers to the "My Content Type" content type.