I want to programmatically add an EventReceiver to some lists.

I have a signed assembly named Customer.Services.Sharepoint where my TestSPItemEventReceiver resides. For debug purpose, it only shows a HTA dialog when fired:

public class TestSPItemEventReceiver : SPItemEventReceiver {
public override void ItemDeleted (SPItemEventProperties properties) { ShowDialogHTA(); }
public override void ItemAdded   (SPItemEventProperties properties) { ShowDialogHTA(); }
public override void ItemUpdated (SPItemEventProperties properties) { ShowDialogHTA(); }

private static void ShowDialogHTA([CallerMemberName]string message = null) {
Process.Start("mshta", "\"javascript:var sh=new ActiveXObject( 'WScript.Shell' ); sh.Popup( 'TestSPItemEventReceiver', 10, 'Debugging', 64 );close()\"");
}
}


[*] The code that I am using to attach my TestSPItemEventReceiver is the follow:

using (var web = new SPSite("http://customer/").OpenWeb()) {

var list = web.Lists["Documents"];

e.Name = "Test_ALL_" + DateTime.Now.ToString("hh-mm-ss");
e.Type =

e.SequenceNumber = 10;
e.Update();

list.Update();
web.Update();

}


Now, my steps to do that are:

1.) >_ gacutil /i "path\to\Customer.Services.Sharepoint.dll"
2.) >_ IISRESET
3.) >_ net stop SPTimerV4
4.) >_ net start SPTimerV4
5.) Run the [*] code


After these steps, specifically at e.Update(); code in [*], my ".ctor" HTA dialog pops up - and it makes me happy! :)

Then, I go to Documents library, like an user and do some additions, deletions and updates, but, TestSPItemEventReceiver are never called again! - and it makes me sad! :(

My evidence that TestSPItemEventReceiver were correctly attached is follow PowerShell script...

$web = Get-SPWeb "http://customer/"$list = $web.Lists["Documents"]$list.EventReceivers | select Name, Type


...and getting the result:

Name                                Type
----                                ----
Test_ALL_10-07-23            ItemDeleted [***]


I think SPEventReceiverType is not expected to be used with bitwise operations [* *] , because of [***]. Anyway, at least I expect that the ItemDeleted event beeing triggered.

Finally, my question: What am I doing wrong???

EDIT: As we can see ItemAdded, ItemUpdated and ItemDeleted, has values 10001, 10002 and 10003. As 10001 | 10002 | 10003 = 10003 my assumption that ItemDeleted must be triggered are right!

You will need to separately associate the Add, Delete and Update event receivers to the list, by passing appropriate parametric values to the code below -

list.EventReceivers.Add


public void Add(SPEventReceiverType receiverType, string assembly, string className);


Note - Adding event receiver to the list is one time activity. Once you have event receiver properly attached it will fire for every time for the action.

Take care not to attach an event receiver twice/more or your code will run as many times as it is attached.

I was wrong. This EventReceiver are firing correctly - It was only a debug mistake from myself.

1.) Where is the HTA dialog?

• Services do not have GUI. This were a stupid idea from myself;

2.) But, the ".ctor" HTA dialog was showed as well.

• I think its because the e.Update(); line was being executed by the Console Application process in an Visual Studio Debug session, not the w3wp or a SharePoint process;

3.) So, what was wrong with my original implementation?

• I am using the System.Web.MVC and for some reason it weren't findable by SharePoint. I added it to GAC and works fine.

NOTE: All this bad practices of "add this and that to GAC" are just for development. Im going to do a elegant feature solution :)