Deploy event receiver without associating with any list
-
10-12-2019 - |
Question
I have an event receiver I developed with Visual Studio that I want to deploy. But in this case I want to deploy it without associating it with any list. (I add the event receiver to a specific list later via PowerShell.)
I know that if I set the Scope
to Site
then the receiver will fire for all lists and libraries. And I know that if I set the Scope
to Web
and provide a value for ListUrl
then it will only bind to the specified list.
However, it seems that if I set the Scope
to Web
and do not specify a ListUrl
, it acts the same as Site
and binds to everything. Here is my code
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers Scope="Web">
<Receiver>
<Name>AutoNumberDocumentItemAdded</Name>
<Type>ItemAdded</Type
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>ArticleManagementWorkflow.AutoNumberDocument.AutoNumberDocument</Class>
<SequenceNumber>10000</SequenceNumber>
<Synchronization>Synchronous</Synchronization>
</Receiver>
</Receivers>
</Elements>
Is there a way to deploy and activate the receiver without it being bound to anything?
Solution
Deploy the event receiver and do not activate the Feature.The elements file only attaches the event receiver to list when feature is activated. Attach the event receiver to list from your powershell afterwards.
OTHER TIPS
There is another option how to add Event Receiver to a specific List - add the Event Receiver to the Content Type.
Steps:
- Create custom Content Type for a List
- Add the Event Receiver to the Content Type
- Add Content Type to a specific List in order to register the Event Receiver
In that case you will be able to control on which List an Event Receiver should be registered.
Please refer a great article Walkthrough 3 – Developing an Event Receiver for a Content Type for a more details.
In your case you don't need a feature.
All you need is a class library with your event receiver class, sign the assembly and deploy it to GAC. Then with PowerShell register the event receiver for the list you want.
How about attaching it to a dummy list which does not actually exist in the web and then later attaching it to the actual list using Powershell.